Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 150, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

ASp.Net: Search for a Particular User in Active Directory

If you are using FomrsAuthentication against ActiveDirectory you might want to search if a User Id exist in Active Directory or not before taking certain action like creating new profile.

  Here is a Code Snippet used to Check for existance of a User in Active Directory. Some of the setting are stored in web.config.

Note: IT is highly recommended that you do not store usernames or passwords in web.config unencrypted here for demonstration I have put them in clear text to connect to AD.

ConfigurationSettings.AppSettings are read from web.config and you can add your own app settings for those variables.

  public bool IsUserExistInActiveDirectory(string UserId)
  {
   bool IsValidLoginName = false;
   string domain = "LDAP://" + ConfigurationSettings.AppSettings["Domain"];
   System.DirectoryServices.DirectoryEntry entry =
    new DirectoryEntry(domain, ConfigurationSettings.AppSettings["ADServiceAccount"], ConfigurationSettings.AppSettings["ADServiceAccountPassword"],
    AuthenticationTypes.Secure);


   DirectorySearcher adSearcher = new DirectorySearcher(entry);
   adSearcher.SearchScope = SearchScope.Subtree;
   adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + UserId + "))";
      SearchResult oResult = adSearcher.FindOne();

   if ( oResult != null)
   {
     IsValidLoginName = true;
   }
           return IsValidLoginName;
  }

Print | posted on Friday, August 26, 2005 12:07 PM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: ASp.Net: Search for a Particular User in Active Directory

thanks a lot, you have saved my career... :)
4/2/2008 9:38 PM | ferry
Gravatar

# re: ASp.Net: Search for a Particular User in Active Directory

Thanks a bunch. The filter property values are no detailed very well in Microsoft. I wish there was a complete listing. This methodology is better than using scripts. Tks again!
9/7/2008 10:57 AM | Eric Underwood
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: