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

2020-11-27 17:12

阅读:479

                                                                    ASP.NET连接LDAP数据库的有关信息

一、封装在DAL层中的获取用户信息的函数

/// 

       /// 按照用户Id查找用户信息

       /// 

       /// 

       /// 

       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;

        }

二、配置文件信息

          

 

         connectionString="LDAP://192.168.1.1/OU=123,DC=123,DC=COM" />

           

                   connectionString="123" />

           

                   connectionString="123" />

  

三、实现层

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

                    }

                }


评论


亲,登录后才可以留言!