如果我设置了.NET DirectoryEntry.Path喜欢的东西:
LDAP://CN=John Smith,OU=Group Name,DC=example,DC=com
一切都很正常,我也得到我需要的DirectoryEntry。 不过,我不知道用户的真正的通用名称(CN)。 我只知道他们的用户名“和John.Smith”。
那么,如何可以查询用户名? 我曾尝试没有成功以下所有:
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字符串进行查询 - 你需要使用的代码为。
就像是:
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();
}
}
在完整的MSDN文档System.DirectoryServices.DirectorySearcher类可以在MSDN上找到-它有很多的附加属性和设置。
如果你在.NET 3.5中,事情变得相当多与程序处理用户和组的强类型库更容易-看到这样优秀的MSDN文章的主题了解更多信息。
希望这可以帮助
渣