How do I query ActiveDirectory using LDAP with a username, not a CN?(如何使用带有用户名而不是 CN 的 LDAP 查询 ActiveDirectory?)
问题描述
如果我将 .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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用带有用户名而不是 CN 的 LDAP 查询 ActiveDirectory?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01