ASP. NET connects LDAP data and extracts relevant data from LDAP (1)

Source: Internet
Author: User
Tags connectionstrings

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 ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.