C# LDAP认证登录类参考

来源:互联网 发布:如何查看笔记本mac地址 编辑:程序博客网 时间:2024/06/08 08:13

写了一个通用的认证类,请看代码

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  private void btnCheck_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.   
  5.             string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());        
  6.             //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";  
  7.   
  8.   
  9.             string TestUserID = txtUserName.Text;  
  10.             string TestUserPwd = txtPwd.Text;  
  11.             LDAPHelper objldap = new LDAPHelper();  
  12.             string strLDAPPath = txtLDAP.Text;  
  13.             string strLDAPAdminName = txtLUserName.Text;  
  14.             string strLDAPAdminPwd = txtLPwd.Text;  
  15.             string strMsg = "";  
  16.             bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd);  
  17.   
  18.   
  19.             if (blRet)  
  20.             {  
  21.                 blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);  
  22.                 if (blRet)  
  23.                 {  
  24.                     strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";  
  25.                 }  
  26.                 else if (!blRet && string.IsNullOrEmpty(strMsg))  
  27.                 {  
  28.                     strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";  
  29.                 }  
  30.             }  
  31.             this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text;  
  32.             MessageBox.Show(strMsg);  
  33.         }  
  34.     }  



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class LDAPHelper  
  2.     {  
  3.         private DirectoryEntry _objDirectoryEntry;  
  4.   
  5.   
  6.         /// <summary>  
  7.         /// 构造函数  
  8.         /// </summary>  
  9.         /// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param>  
  10.         /// <param name="authUserName">连接用户名,例如"cn=root,dc=***,dc=com"</param>  
  11.         /// <param name="authPWD">连接密码</param>  
  12.         public bool OpenConnection(string LADPath, string authUserName, string authPWD)  
  13.         {    //创建一个连接   
  14.              _objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None);  
  15.   
  16.   
  17.              if (null == _objDirectoryEntry)  
  18.              {  
  19.                  return false;  
  20.              }  
  21.              else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > 0)  
  22.              {  
  23.                  return true;  
  24.              }  
  25.              return false;  
  26.         }  
  27.   
  28.   
  29.         /// <summary>  
  30.         /// 检测一个用户和密码是否正确  
  31.         /// </summary>  
  32.         /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param>  
  33.         /// <param name="TestUserID">testuserid</param>  
  34.         /// <param name="TestUserPwd">testuserpassword</param>  
  35.         /// <param name="ErrorMessage"></param>  
  36.         /// <returns></returns>  
  37.         public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)  
  38.         {  
  39.             bool blRet = false;  
  40.             try  
  41.             {  
  42.                 //创建一个检索  
  43.                 DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry);  
  44.                 //过滤名称是否存在  
  45.                 deSearch.Filter =strLDAPFilter;  
  46.                 deSearch.SearchScope = SearchScope.Subtree;  
  47.   
  48.   
  49.                 //find the first instance   
  50.                 SearchResult objSearResult = deSearch.FindOne();  
  51.   
  52.   
  53.                 //如果用户密码为空  
  54.                 if (string.IsNullOrEmpty(TestUserPwd))  
  55.                 {  
  56.                     if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0)  
  57.                     {  
  58.                         blRet = true;  
  59.                     }  
  60.                 }  
  61.                 else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))  
  62.                 {  
  63.                     //获取用户名路径对应的用户uid  
  64.                     int pos = objSearResult.Path.LastIndexOf('/');  
  65.                     string uid = objSearResult.Path.Remove(0, pos + 1);  
  66.                     DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None);  
  67.                     if (null != objUserEntry && objUserEntry.Properties.Count > 0)  
  68.                     {  
  69.                         blRet = true;  
  70.                     }  
  71.                 }  
  72.             }  
  73.             catch (Exception ex)  
  74.             {  
  75.                 if (null != _objDirectoryEntry)  
  76.                 {  
  77.                     _objDirectoryEntry.Close();  
  78.                 }  
  79.                 ErrorMessage = "检测异常:"+ex.StackTrace;  
  80.             }  
  81.             return blRet;  
  82.         }  
  83.   
  84.   
  85.   
  86.   
  87.         /// <summary>  
  88.         /// 关闭连接  
  89.         /// </summary>  
  90.         public void closeConnection()  
  91.         {  
  92.             if (null != _objDirectoryEntry)  
  93.             {  
  94.                 _objDirectoryEntry.Close();  
  95.             }  
  96.         }  
  97.     }  

调用



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  private void btnCheck_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.   
  5.             string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());        
  6.             //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";  
  7.   
  8.   
  9.             string TestUserID = txtUserName.Text;  
  10.             string TestUserPwd = txtPwd.Text;  
  11.             LDAPHelper objldap = new LDAPHelper();  
  12.             string strLDAPPath = txtLDAP.Text;  
  13.             string strLDAPAdminName = txtLUserName.Text;  
  14.             string strLDAPAdminPwd = txtLPwd.Text;  
  15.             string strMsg = "";  
  16.             bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd);  
  17.   
  18.   
  19.             if (blRet)  
  20.             {  
  21.                 blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);  
  22.                 if (blRet)  
  23.                 {  
  24.                     strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";  
  25.                 }  
  26.                 else if (!blRet && string.IsNullOrEmpty(strMsg))  
  27.                 {  
  28.                     strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";  
  29.                 }  
  30.             }  
  31.             this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text;  
  32.             MessageBox.Show(strMsg);  
  33.         }  
  34.     }  

实例下载:http://download.csdn.net/detail/paolei/6740833

LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP。它是基于X.500标准的,但是简单多了并且可以根据需要定制。与X.500不同,LDAP支持TCP/IP,这对访问Internet是必须的。LDAP的核心规范在RFC中都有定义,所有与LDAP相关的RFC都可以在LDAPman RFC网页中找到。

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. bool checkResult = false;  
  2.                 try  
  3.                 {  
  4.                     string username = Request.Params.Get("username");  
  5.                     string userpwd = Request.Params.Get("userpwd");  
  6.                     string strLADPath = "LDAP://OU=事业部,DC=HOLD,DC=Company,DC=COM";  
  7.                      
  8.                     DirectoryEntry objEntry = new DirectoryEntry(strLADPath);  
  9.                     objEntry.AuthenticationType = AuthenticationTypes.None;  
  10.   
  11.                     DirectorySearcher deSearch = new DirectorySearcher(objEntry);  
  12.                     //过滤名称是否存在  
  13.                     deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";  
  14.                     deSearch.SearchScope = SearchScope.Subtree;  
  15.                     //find the first instance   
  16.                     SearchResult results = deSearch.FindOne();  
  17.                     //check username & userpwd  
  18.                     if (null != results)  
  19.                     {  
  20.                         DirectoryEntry objUserEntry = new DirectoryEntry(results.Path, username, userpwd);  
  21.                         if (null != objUserEntry && null != objUserEntry.Properties  
  22.                             && objUserEntry.Properties.Contains("cn"))  
  23.                         {  
  24.                             checkResult = true;  
  25.                         }  
  26.                     }  
  27.   
  28.                     Response.Write("认证结果:" + checkResult.ToString());  
  29.                 }  
  30.                 catch (System.Exception ex)  
  31.                 {  
  32.                     Response.Write("认证异常"+ex.StackTrace);  
  33.                     Response.Write("认证结果:" + checkResult.ToString());  
  34.                 }  



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  private void btnCheck_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.   
  5.             string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());        
  6.             //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";  
  7.   
  8.   
  9.             string TestUserID = txtUserName.Text;  
  10.             string TestUserPwd = txtPwd.Text;  
  11.             LDAPHelper objldap = new LDAPHelper();  
  12.             string strLDAPPath = txtLDAP.Text;  
  13.             string strLDAPAdminName = txtLUserName.Text;  
  14.             string strLDAPAdminPwd = txtLPwd.Text;  
  15.             string strMsg = "";  
  16.             bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd);  
  17.   
  18.   
  19.             if (blRet)  
  20.             {  
  21.                 blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);  
  22.                 if (blRet)  
  23.                 {  
  24.                     strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";  
  25.                 }  
  26.                 else if (!blRet && string.IsNullOrEmpty(strMsg))  
  27.                 {  
  28.                     strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";  
  29.                 }  
  30.             }  
  31.             this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text;  
  32.             MessageBox.Show(strMsg);  
  33.         }  
  34.     }  
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class LDAPHelper  
  2.     {  
  3.         private DirectoryEntry _objDirectoryEntry;  
  4.   
  5.   
  6.         /// <summary>  
  7.         /// 构造函数  
  8.         /// </summary>  
  9.         /// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param>  
  10.         /// <param name="authUserName">连接用户名,例如"cn=root,dc=***,dc=com"</param>  
  11.         /// <param name="authPWD">连接密码</param>  
  12.         public bool OpenConnection(string LADPath, string authUserName, string authPWD)  
  13.         {    //创建一个连接   
  14.              _objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None);  
  15.   
  16.   
  17.              if (null == _objDirectoryEntry)  
  18.              {  
  19.                  return false;  
  20.              }  
  21.              else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > 0)  
  22.              {  
  23.                  return true;  
  24.              }  
  25.              return false;  
  26.         }  
  27.   
  28.   
  29.         /// <summary>  
  30.         /// 检测一个用户和密码是否正确  
  31.         /// </summary>  
  32.         /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param>  
  33.         /// <param name="TestUserID">testuserid</param>  
  34.         /// <param name="TestUserPwd">testuserpassword</param>  
  35.         /// <param name="ErrorMessage"></param>  
  36.         /// <returns></returns>  
  37.         public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)  
  38.         {  
  39.             bool blRet = false;  
  40.             try  
  41.             {  
  42.                 //创建一个检索  
  43.                 DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry);  
  44.                 //过滤名称是否存在  
  45.                 deSearch.Filter =strLDAPFilter;  
  46.                 deSearch.SearchScope = SearchScope.Subtree;  
  47.   
  48.   
  49.                 //find the first instance   
  50.                 SearchResult objSearResult = deSearch.FindOne();  
  51.   
  52.   
  53.                 //如果用户密码为空  
  54.                 if (string.IsNullOrEmpty(TestUserPwd))  
  55.                 {  
  56.                     if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0)  
  57.                     {  
  58.                         blRet = true;  
  59.                     }  
  60.                 }  
  61.                 else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))  
  62.                 {  
  63.                     //获取用户名路径对应的用户uid  
  64.                     int pos = objSearResult.Path.LastIndexOf('/');  
  65.                     string uid = objSearResult.Path.Remove(0, pos + 1);  
  66.                     DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None);  
  67.                     if (null != objUserEntry && objUserEntry.Properties.Count > 0)  
  68.                     {  
  69.                         blRet = true;  
  70.                     }  
  71.                 }  
  72.             }  
  73.             catch (Exception ex)  
  74.             {  
  75.                 if (null != _objDirectoryEntry)  
  76.                 {  
  77.                     _objDirectoryEntry.Close();  
  78.                 }  
  79.                 ErrorMessage = "检测异常:"+ex.StackTrace;  
  80.             }  
  81.             return blRet;  
  82.         }  
  83.   
  84.   
  85.   
  86.   
  87.         /// <summary>  
  88.         /// 关闭连接  
  89.         /// </summary>  
  90.         public void closeConnection()  
  91.         {  
  92.             if (null != _objDirectoryEntry)  
  93.             {  
  94.                 _objDirectoryEntry.Close();  
  95.             }  
  96.         }  
  97.     }  
0 0
原创粉丝点击