问题描述
如果我将 .NET DirectoryEntry.Path 设置为:
If I set the .NET DirectoryEntry.Path to something like:
LDAP://CN=John Smith,OU=Group Name,DC=example,DC=com
一切都很好,我得到了我需要的 DirectoryEntry.但是,我不知道用户的真实通用名 (CN).我只知道他们的用户名John.Smith".
Everything works great, and I get the DirectoryEntry I need. However, I don't know the user's true Common Name (CN). I only know their username, "John.Smith".
那么,如何查询用户名?我尝试了以下所有没有成功:
So, how can I query the username? I have tried all the following without success:
LDAP://CN=John.Smith,OU=Group Name,DC=example,DC=com
LDAP://sAMAccountName=John.Smith,OU=Group Name,DC=example,DC=com
LDAP://userPrincipalName=John.Smith,OU=Group Name,DC=example,DC=com
LDAP://userPrincipalName=John.Smith@example.com,OU=Group Name,DC=example,DC=com
LDAP://uid=John.Smith,OU=Group Name,DC=example,DC=com
LDAP://o=John.Smith,OU=Group Name,DC=example,DC=com
推荐答案
您不能仅仅通过创建 LDAP 字符串进行查询 - 您需要为此使用代码.
You can't just query by means of creating an LDAP string - you'll need to use code for that.
类似:
DirectoryEntry deRoot = new DirectoryEntry("LDAP://yourserver/CN=Users,dc=YourCompany,dc=com");
DirectorySearcher dsFindUser = new DirectorySearcher(deRoot);
dsFindUser.SearchScope = SearchScope.SubTree;
dsFindUser.PropertiesToLoad.Add("sn"); // surname = last name
dsFindUser.PropertiesToLoad.Add("givenName"); // first name
dsFindUser.Filter = string.Format("(&(objectCategory=Person)(anr={0}))", yourUserName);
SearchResult rseult = dsFindUser.FindOne();
if(result != null)
{
if(result.Properties["sn"] != null)
{
string lastName = result.Properties["sn"][0].ToString();
}
if(result.Properties["givenName"] != null)
{
string lastName = result.Properties["givenName"][0].ToString();
}
}
System.DirectoryServices.DirectorySearcher<上的完整 MSDN 文档/a> 类可以在 MSDN 上找到 - 它有很多额外的属性和设置.
The full MSDN documentation on the System.DirectoryServices.DirectorySearcher class can be found on MSDN - it has lots of additional properties and settings.
如果您使用的是 .NET 3.5,那么使用强类型的例程库来处理用户和组会变得相当容易 - 请参阅此优秀的 有关该主题的 MSDN 文章 了解更多信息.
If you're on .NET 3.5, things have gotten quite a bit easier with a strongly-typed library of routines for handling users and groups - see this excellent MSDN article on the topic for more info.
希望对你有帮助
马克
这篇关于如何使用带有用户名而不是 CN 的 LDAP 查询 ActiveDirectory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!