[英]C# Active Directory DirectorySearcher is only finding my own user
我有一个函数,用于查找与给定名称匹配的任何用户的displayName和mail字段。 到目前为止,它仍然有效,但是它只会返回我以其身份登录的用户。
我已经尝试过不同版本的DirectorySearcher
,例如:
new DirectorySearcher("LDAP://my.domain");
new DirectorySearcher();
new DirectorySearcher("LDAP://my.domain/(&(objectCategory=person)(objectClass=user)(anr={0}))");
我还尝试了不同的路径格式和AuthenticationTypes。
我的职能:
/// <summary>
/// Returns a Dictionary of Names and Emails that match the given name
/// </summary>
/// <param name="name">Name to search for</param>
/// <param name="domain">Domain to log in to</param>
/// <param name="username">Username for login into AD</param>
/// <param name="pwd">Password for login into AD</param>
/// <param name="count">returns the number of results found</param>
/// <returns>Dictionary containing the Names and Emails of the users matched</returns>
public Dictionary<string, string> GetPersonsEmailsByName(string name, string domain, string username, string pwd, out int count)
{
count = 0;
if (String.IsNullOrEmpty(name)) return new Dictionary<string, string>();
try
{
string domainAndUsername = String.Format(@"{0}\{1}", domain, username);
using (DirectoryEntry root = new DirectoryEntry("LDAP://my.domain", domainAndUsername, pwd, AuthenticationTypes.Delegation))
{
DirectorySearcher search = new DirectorySearcher(root);
search.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(anr={0}))", name);
search.SearchScope = SearchScope.Subtree;
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("mail");
var dict = new Dictionary<string, string>();
SearchResultCollection result = search.FindAll();
count = result.Count;
foreach (SearchResult sr in result)
{
var de = sr.GetDirectoryEntry();
dict.Add((string)de.Properties["displayName"].Value, (string)de.Properties["mail"].Value);
}
return dict;
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining persons: " + ex.Message);
}
}
这是使用此功能的示例输出:
User: john_doe
Pass:
Auth OK
Name: Mike
Results: 0
Name: carl
Results: 0
Name: jo
Results: 1
John Doe: j_doe@domain.com
Name: john
Results: 1
John Doe: j_doe@domain.com
Name: j
Results: 1
John Doe: j_doe@domain.com
Name:
我所能得到的就是我自己的名字。
在此先感谢您的帮助。
原来这都是我的错。 我在代码的较早部分进行了另一个功能的登录,这在我不知不觉中将我使用的路径变量更改为登录用户的cn,这就是为什么除了登录用户之外,它没有给我任何其他结果的原因。
因此,此方法完全可以正常工作!
我很抱歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.