使用C#检查Active Directory中是否存在UserID

我们如何检查USERID中是否存在USERID。

我有LDAP String和UserID,我可以找到Active Directory中是否存在该UserID。 我在ASP.NET Web应用程序(.NET 3.5)中使用它

您可以执行某些操作(使用您要进行身份validation的域替换域或完全删除参数):

public bool DoesUserExist(string userName) { using (var domainContext = new PrincipalContext(ContextType.Domain, "DOMAIN")) { using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName)) { return foundUser != null; } } } 

实现检查用户是否存在。 它来自System.DirectoryServices.AccountManagement命名空间和程序集。

您可以在http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx上找到更多信息。

您可能希望更多地检查PrincipalContext,因为它有用于validation用户凭据等的有趣方法。

我会使用System.DirectoryServices.AccountManagement命名空间。

 string UserID = "grhm"; bool userExists = false; using (var ctx = new PrincipalContext(ContextType.Domain)) { using (var user = UserPrincipal.FindByIdentity(ctx, UserID)) { if (user != null) { userExists = true; user.Dispose(); } } } 

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/bb344891.aspx