rst*_*use 22
@KenL几乎让我在那里.我还必须设置DirectoryEntry的AuthenticationType 以使其工作.另外,要注意你如何使用通配符(Kleene Stars).
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://some.ldap.server.com");
rootEntry.AuthenticationType = AuthenticationTypes.None; //Or whatever it need be
DirectorySearcher searcher = new DirectorySearcher(rootEntry);
var queryFormat = "(&(objectClass=user)(objectCategory=person)(|(SAMAccountName=*{0}*)(cn=*{0}*)(gn=*{0}*)(sn=*{0}*)(email=*{0}*)))";
searcher.Filter = string.Format(queryFormat, searchString);
foreach(SearchResult result in searcher.FindAll())
{
Console.WriteLine("account name: {0}", result.Properties["samaccountname"].Count > 0 ? result.Properties["samaccountname"][0] : string.Empty);
Console.WriteLine("common name: {0}", result.Properties["cn"].Count > 0 ? result.Properties["cn"][0] : string.Empty);
}
Run Code Online (Sandbox Code Playgroud)
JPB*_*anc 19
第一个响应元素,使用ADSI(旧时尚)
如何使用C#在Active Directory上几乎所有(使用ADSI)
第二个响应元素,开始.NET 3.5 Microsoft引入了" Principal "和" AccountManagement ".
如何使用C#在Active Directory上执行几乎所有操作(使用AccountManagement)
第三响应元素,您可以使用System.DirectoryServices.Protocols(S.DS.P)的低级(本机LDAP)协议.
备注:如果您对如何从本机代码查询活动目录感兴趣,可以查看RFC C23中指定的LDAP C-Binding API,Microsoft支持它,请参阅轻量级目录访问协议(LDAP)的MS策略.您将在轻量级目录访问协议中找到Microsoft API的使用和参考手册.
Ken*_*enL 13
代码方面它比你想象的要简单得多.您需要创建与目录的连接,设置搜索器,然后按属性名称搜索.
DirectoryEntry entry = new DirectoryEntry("LDAP://MyDomain.com");
DirectorySearcher dSearch = new DirectorySearcher(entry);
dSearch.Filter = "(&(objectCategory=person)(objectClass=user)(" + SType + "=" + Name + "))";
Run Code Online (Sandbox Code Playgroud)
SType是名称类型,Name是实际用户名