asp.net连接LDAP数据,并从LDAP中取出相关数据(1)

 

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

                    }

                }

posted @ 2014-05-05 10:10  Alanblog  阅读(1377)  评论(0编辑  收藏  举报