asp.net串連LDAP資料,並從LDAP中取出相關資料(1)

來源:互聯網
上載者:User

標籤:des   class   tar   ext   c   get   

 

                                                                    ASP.NET串連LDAP資料庫的有關資訊

一、封裝在DAL層中的擷取使用者資訊的函數

/// <summary>

       /// 按照使用者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個串連資料庫的資訊寫在設定檔裡

           DirectoryEntry  deuser;  //定義變數

           try

           {

               DirectoryEntry de = newDirectoryEntry (path, pname, pwd, AuthenticationTypes.Secure);

               DirectorySearcher deSearch = newDirectorySearcher(de); //串連LDAP資料庫

               deSearch.Filter = "(&(objectClass=userinfo)(LOGINNAME=" + username + "))";  //篩選比對

//上面這句話修改為:mySearcher.Filter = "(&(objectClass=userinfo)(&(LOGINNAME=" + txtUserId.Text + ")(LOGINPASSWORD=" + txtUserPwd.Text + ")))";//作為登入是使用者帳號和密碼認證

               deSearch.SearchScope = SearchScope.Subtree;

               SearchResult result = deSearch.FindOne(); //篩選比對得出一個結果,儲存在result中

               if (result != null)

               {

                   deuser = result.GetDirectoryEntry(); //得到篩選結果,並賦值給deuser中

                   return deuser;

               }

               else

               {

                returnnull;

                }

             }

        catch(Exception ex)

        {            

           // LogManage.SaveInfo(ex.ToString());

            returnnull;

        }

二、設定檔資訊

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

三、實現層

1、頁面資訊

使用者id:(textbox框)

使用者名稱稱:(textbox框)
使用者密碼:(textbox框)
電子郵箱:(textbox框)
ButtonID="butquchu"(讀取資料按鈕)

2、事件函數代碼

protectedvoid butquchu_Click(object sender, EventArgs e)

        {

            ldapDAO ld= newldapDAO ();

 

            string username = Session["LOGINNAME"].ToString(); //根據上一頁的登入資訊擷取使用者帳號,儲存在session中。

            labname.Text = username;

            DirectoryEntry de = ld.GetUser(username); //調用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();

                    }

                }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.