Find user in Active Directory

24/10/2013 16:46

Find User in Active Directory

 

Add References to

System.DirectoryServices;

System.DirectoryServices.AccountManagement;

 

using System.DirectoryServices;

using System.DirectoryServices.AccountManagement;

 

public string GetAdUser(string userName)

{

    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))

    {

        string controller = context.ConnectedServer;

 

        UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, userName);

        return userPrincipal.UserPrincipalName;

    }

}