我正在尝试在创建之前检查Active Directory中是否存在用户。我正在使用以下代码:
private static DirectoryEntry FindActiveDirectoryUser(string userName, string domainName)
{
using (DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainName))
{
using (DirectorySearcher searcher = new DirectorySearcher(domain))
{
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.Filter = "(sAMAAccountName=" + userName + ")";
return searcher.FindOne().GetDirectoryEntry();
}
}
}
我收到了错误
A referral was returned from the server.
对于变量userName和domainName,我尝试了FQDN和2000之前的用户名(例如DOMAIN \ User),以及简单的域名和用户名。
有谁知道如何解决这个问题?
答案 0 :(得分:2)
试试这个。它会起作用
public GetUserByLoginName(String userName)
{
try
{
using (HostingEnvironment.Impersonate())
{
// This code runs as the application pool user
_directoryEntry = null;
string path = "LDAP://xxx.local/DC=xxx,DC=xxx"; //your LDAP Address
DirectorySearcher directorySearch = new DirectorySearcher(path);
directorySearch.Filter = "(&(objectClass=user)(SAMAccountName=" + userName + "))";
SearchResult results = directorySearch.FindOne();
}
}
catch (Exception ex)
{
return null;
}
}
答案 1 :(得分:0)