Geeks With Blogs
Jawad Khan Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.

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;
  }

Posted on Friday, August 26, 2005 12:07 PM ASP.NET | Back to top


Comments on this post: ASp.Net: Search for a Particular User in Active Directory

# re: ASp.Net: Search for a Particular User in Active Directory
Requesting Gravatar...
thanks a lot, you have saved my career... :)
Left by ferry on Apr 02, 2008 9:38 PM

# re: ASp.Net: Search for a Particular User in Active Directory
Requesting Gravatar...
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!
Left by Eric Underwood on Sep 07, 2008 10:57 AM

# re: ASp.Net: Search for a Particular User in Active Directory
Requesting Gravatar...
please help me to mek connection of Ldap with asp.net!!i am newby..:)..so any help would be appreciated.!!!can u pls temme what to write in web.config for making connection ??
Left by Mahak on Apr 15, 2010 7:12 AM

Your comment:
 (will show your gravatar)
 


Copyright © Jawad Khan | Powered by: GeeksWithBlogs.net | Join free