使用C#在Active Directory中检查用户名是否存在是否存在、用户名、Active、Directory

2023-09-09 21:34:09 作者:青衫不改旧人还

我们如何检查用户标识是否存在于Active Directory或没有。

How can we check whether the USERID exists in Active Directory or not.

我有LDAP字符串和用户名,我可以发现,用户名是否存在于Active Directory或不。我用这对ASP.NET Web应用程序(.NET 3.5)

I have LDAP String and UserID, can I find whether that UserID exists in Active Directory or not. I am using this for ASP.NET Web Application (.NET 3.5)

推荐答案

您可以做沿(与你验证对域替代域或完全移除参数)线的东西:

You can do something along the lines of (replacing domain with the domain you're authenticating against or removing the parameter altogether):

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 命名空间和组件。

To achieve checking for if a user exists. This comes from the System.DirectoryServices.AccountManagement namespace and assembly.

您可以找到http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx

您可能要检查更进PrincipalContext因为它有趣的方法来验证用户凭据和这样的。

You may want to check more into PrincipalContext as it has interesting methods for authenticating user credentials and such.