Connecting Tech Pros Worldwide Forums | Help | Site Map

C# - Authenticate users thorough Active Directory

JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 110
#1: Nov 10 '08
Hi,

I'm developing a windows application using VC#, I need to authenticate user throught their credentials in the Active Dirctory,
I try to let the login form "user name" take automaticaly the username in the active directory, I don't know whether I wrote is right or wrong, but at any rate it doesn't work.
here is my code:

Expand|Select|Wrap|Line Numbers
  1.         public static string GetProperty(SearchResult searchResult, string PropertyName)
  2.         {
  3.            if(searchResult.Properties.Contains(PropertyName))
  4.            {
  5.             return searchResult.Properties[PropertyName][0].ToString() ;
  6.            }
  7.            else
  8.            {
  9.             return string.Empty;
  10.            }
  11.         }
  12.  
  13.         private void frmAuthentication_Load(object sender, EventArgs e)
  14.         {
  15.             DirectoryEntry entry = new DirectoryEntry("LDAP://192.168.1.110");
  16.             DirectorySearcher Dsearch = new DirectorySearcher(entry);
  17.             string Name = "Richmond";
  18.             Dsearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
  19.  
  20.             // get all entries from the active directory.
  21.             // Last Name, name, initial, homepostaladdress, title, company etc..
  22.             try
  23.             {
  24.                 foreach (SearchResult sResultSet in Dsearch.FindAll())
  25.                 {
  26.                     txtUserName.Text = (GetProperty(sResultSet, "cn"));
  27.                 }
  28.             }
  29.             catch(Exception ex)
  30.             {
  31.                 ex.Message.ToString();
  32.             }
  33.         }
  34.  

Reply