Asp. NET connection to the LDAP database
The function of obtaining user information encapsulated in the DAL layer
<summary>
Find user information by user ID
</summary>
<param name= "UserId" ></param>
<returns></returns>
Publicdirectoryentry GetUser (string username)
{
String path = system.configuration.configurationmanager.connectionstrings["path"]. ConnectionString;
string pname = system.configuration.configurationmanager.connectionstrings["PName"]. ConnectionString;
string pwd = system.configuration.configurationmanager.connectionstrings["pwd"]. ConnectionString;
3 Connection database information is written in the configuration file
DirectoryEntry Deuser; Defining variables
Try
{
DirectoryEntry de = newdirectoryentry (path, pname, PWD, authenticationtypes.secure);
DirectorySearcher desearch = Newdirectorysearcher (DE); Connecting to the LDAP database
Desearch.filter = "(& (Objectclass=userinfo) (loginname=" + username + "))"; Filter the comparison
The above sentence is modified to: Mysearcher.filter = "(& (Objectclass=userinfo) (& (loginname=" + Txtuserid.text + ") (loginpassword=" + Txtuserpwd.text + ")))";//As login is User account and password authentication
Desearch.searchscope = Searchscope.subtree;
SearchResult result = Desearch.findone (); A result of the filter comparison, stored in result
if (result! = NULL)
{
Deuser = result. Getdirectoryentry (); Get the filtered results and assign them to the Deuser
return deuser;
}
Else
{
Returnnull;
}
}
catch (Exception ex)
{
Logmanage.saveinfo (ex. ToString ());
Returnnull;
}
Ii. configuration file Information
<connectionStrings>
<add name= "Path"
connectionstring= "Ldap://192.168.1.1/ou=123,dc=123,dc=com"/>
<add name= "PName"
connectionstring= "123"/>
<add name= "pwd"
connectionstring= "123"/>
</connectionStrings>
Third, the realization layer
1. Page Information
User id: (textbox box)
User name: (textbox box)
User password: (textbox box)
E-mail: (textbox box)
Buttonid= "Butquchu" (Read Data button)
2. Event function code
Protectedvoid Butquchu_click (object sender, EventArgs e)
{
Ldapdao ld= Newldapdao ();
String username = session["LOGINNAME"]. ToString (); Get the user account based on the login information on the previous page, stored in the session.
Labname. Text = Username;
DirectoryEntry de = ld. GetUser (username); Call the Get user information function in Ldapdao
if (de! = null)
{
if (de! = null)
{
if (DE. properties["USERID"]. Value = null)
{
Txtuserid. Text = de. properties["USERID"]. Value.tostring ();
}
if (DE. properties["LOGINNAME"]. Value = null)
{
txtUserName. Text = de. properties["LOGINNAME"]. Value.tostring ();
}
if (DE. properties["Loginpassword"]. Value = null)
{
Txtpwd. Text = de. properties["Loginpassword"]. Value.tostring ();
}
if (DE. properties["EMAIL"]. Value = null)
{
Txtmail. Text = de. properties["EMAIL"]. Value.tostring ();
}
}