Check UserID exists in Active Directory using C#

后端 未结 2 1087
长发绾君心
长发绾君心 2020-12-13 09:45

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

I have LDAP String and UserID, can I find whether that UserID exists in Active Directory or no

2条回答
  •  再見小時候
    2020-12-13 10:29

    I would use the System.DirectoryServices.AccountManagement namespace.

    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();
            }
        }
    }
    

    See http://msdn.microsoft.com/en-us/library/bb344891.aspx for more info

提交回复
热议问题