Friday, June 15, 2012

c# check user exists in active directory

Here is the C# code to check if user exists in Active directory or not :


using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "DomainName"))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, "LoginName"))
{
     bool userExists = (user != null);
     if (userExists)
     {
       Console.WriteLine("User exists");
       Console.WriteLine(user.EmailAddress);
     }
     else
     {
      Console.WriteLine("User doesn't exist");
     }
}
}

1 comment: