我实现了System.DirectoryServices.AccountManagement来对我的for应用程序进行身份验证,通过给定的用户名查找用户(UserPrincipal)。然而,我有几种情况下,我需要得到的AD帐户只给一个employeeID。有什么好方法可以让UserPrincipal (甚至只有sAMAccountName)在AccountManagement中获得employeeID呢?
我目前正在用用户名来抓取用户:
PrincipalContext adAuth = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
//get user
UserPrincipal usr = UserPrincipal.FindByIdentity(adAuth, username);我一直在寻找,似乎找不到答案来确认这是否可以做到。如果我不能用AccountManagement做这件事,那么获得sAMAccountName给employeeID的最佳方法是什么?
发布于 2015-12-18 00:33:22
您不需要在System.DirectoryServices.AccountManagement命名空间之外。
UserPrincipal searchTemplate = new UserPrincipal(adAuth);
searchTemplate.EmployeeID = "employeeID";
PrincipalSearcher ps = new PrincipalSearcher(searchTemplate);
UserPrincipal user = (UserPrincipal)ps.FindOne();在本例中,如果没有找到用户,则user对象将为null。如果要查找UserPrinicipal对象的集合,可以在PrincipalSearcher (ps)对象上使用FindAll方法。
还请注意,FindOne方法返回一个UserPrincipal对象,但我们知道它实际上是一个UserPrincipal,应该以这种方式处理( part ),因为UserPrincipal是搜索筛选器的一部分。
发布于 2014-05-28 17:44:27
因此,我找到了一种使用System.DirectoryServices的方法,如下所示,但它似乎相当冗长:
string username = "";
DirectoryEntry entry = new DirectoryEntry(_path);
//search for a DirectoryEntry based on employeeID
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(employeeID=" + empID + ")";
//username
search.PropertiesToLoad.Add("sAMAccountName");
SearchResult result = search.FindOne();
//get sAMAccountName property
username = result.Properties["sAMAccountName"][0].ToString();当然,我可以将它用于其他属性,但我非常喜欢AccountManagement中的强类型属性。
https://stackoverflow.com/questions/23916328
复制相似问题