How can you find a user in active directory from C#?

后端 未结 8 675
时光取名叫无心
时光取名叫无心 2020-12-24 08:13

I\'m trying to figure out how to search AD from C# similarly to how \"Find Users, Contacts, and Groups\" works in the Active Directory Users and Computers tool. I have a str

8条回答
  •  死守一世寂寞
    2020-12-24 08:44

    Got this from the Joe Kaplan and Ethan Wilansky Article Use this Using (from referencing the System.DirectoryServices.AccountManagement dll):

    using System.DirectoryServices.AccountManagement;
    
    private bool CheckUserinAD(string domain, string username)
    {
        PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);
        UserPrincipal user = new UserPrincipal(domainContext);
        user.Name = username;
        PrincipalSearcher pS = new PrincipalSearcher();
        pS.QueryFilter = user;
        PrincipalSearchResult results = pS.FindAll();
        if (results != null && results.Count() > 0)
            return true;
        return false;
    }
    

提交回复
热议问题