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:
-
public static string GetProperty(SearchResult searchResult, string PropertyName)
-
{
-
if(searchResult.Properties.Contains(PropertyName))
-
{
-
return searchResult.Properties[PropertyName][0].ToString() ;
-
}
-
else
-
{
-
return string.Empty;
-
}
-
}
-
-
private void frmAuthentication_Load(object sender, EventArgs e)
-
{
-
DirectoryEntry entry = new DirectoryEntry("LDAP://192.168.1.110");
-
DirectorySearcher Dsearch = new DirectorySearcher(entry);
-
string Name = "Richmond";
-
Dsearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
-
-
// get all entries from the active directory.
-
// Last Name, name, initial, homepostaladdress, title, company etc..
-
try
-
{
-
foreach (SearchResult sResultSet in Dsearch.FindAll())
-
{
-
txtUserName.Text = (GetProperty(sResultSet, "cn"));
-
}
-
}
-
catch(Exception ex)
-
{
-
ex.Message.ToString();
-
}
-
}
-