Check UserID exists in Active Directory




Please add the System.DirectoryServices.AccountManagement namespace and assembly to your code.

public string DoesUserExist(string userName)
        { 
            using (var domainContext = new PrincipalContext(ContextType.Domain, "spl"))
            {
                using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
                {
                    if (foundUser != null)
                        return foundUser.Name;
                    else
                        return "User Does not exist ";
                }
            }        }

Comments

Popular posts from this blog

WCF interview questions

The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program

what is Event Cache table in sharepoint