public class LDAPHelper { private DirectoryEntry _objDirectoryEntry; /// <summary> /// 構造函數 /// </summary> /// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param> /// <param name="authUserName">鏈接用戶名,例如"cn=root,dc=***,dc=com"</param> /// <param name="authPWD">鏈接密碼</param> public bool OpenConnection(string LADPath, string authUserName, string authPWD) { //建立一個鏈接 _objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None); if (null == _objDirectoryEntry) { return false; } else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > 0) { return true; } return false; } /// <summary> /// 檢測一個用戶和密碼是否正確 /// </summary> /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param> /// <param name="TestUserID">testuserid</param> /// <param name="TestUserPwd">testuserpassword</param> /// <param name="ErrorMessage"></param> /// <returns></returns> public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage) { bool blRet = false; try { //建立一個檢索 DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry); //過濾名稱是否存在 deSearch.Filter =strLDAPFilter; deSearch.SearchScope = SearchScope.Subtree; //find the first instance SearchResult objSearResult = deSearch.FindOne(); //若是用戶密碼爲空 if (string.IsNullOrEmpty(TestUserPwd)) { if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0) { blRet = true; } } else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path)) { //獲取用戶名路徑對應的用戶uid int pos = objSearResult.Path.LastIndexOf('/'); string uid = objSearResult.Path.Remove(0, pos + 1); DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None); if (null != objUserEntry && objUserEntry.Properties.Count > 0) { blRet = true; } } } catch (Exception ex) { if (null != _objDirectoryEntry) { _objDirectoryEntry.Close(); } ErrorMessage = "檢測異常:"+ex.StackTrace; } return blRet; } /// <summary> /// 關閉鏈接 /// </summary> public void closeConnection() { if (null != _objDirectoryEntry) { _objDirectoryEntry.Close(); } } }
寫了一個通用的認證類,請看代碼函數
private void btnCheck_Click(object sender, EventArgs e) { string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim()); //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; string TestUserID = txtUserName.Text; string TestUserPwd = txtPwd.Text; LDAPHelper objldap = new LDAPHelper(); string strLDAPPath = txtLDAP.Text; string strLDAPAdminName = txtLUserName.Text; string strLDAPAdminPwd = txtLPwd.Text; string strMsg = ""; bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd); if (blRet) { blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg); if (blRet) { strMsg = "檢測用戶名" + TestUserID + "和密碼" + TestUserPwd + "成功"; } else if (!blRet && string.IsNullOrEmpty(strMsg)) { strMsg = "檢測用戶名" + TestUserID + "和密碼" + TestUserPwd + "失敗"; } } this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text; MessageBox.Show(strMsg); } }
public class LDAPHelper { private DirectoryEntry _objDirectoryEntry; /// <summary> /// 構造函數 /// </summary> /// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param> /// <param name="authUserName">鏈接用戶名,例如"cn=root,dc=***,dc=com"</param> /// <param name="authPWD">鏈接密碼</param> public bool OpenConnection(string LADPath, string authUserName, string authPWD) { //建立一個鏈接 _objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None); if (null == _objDirectoryEntry) { return false; } else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > 0) { return true; } return false; } /// <summary> /// 檢測一個用戶和密碼是否正確 /// </summary> /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param> /// <param name="TestUserID">testuserid</param> /// <param name="TestUserPwd">testuserpassword</param> /// <param name="ErrorMessage"></param> /// <returns></returns> public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage) { bool blRet = false; try { //建立一個檢索 DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry); //過濾名稱是否存在 deSearch.Filter =strLDAPFilter; deSearch.SearchScope = SearchScope.Subtree; //find the first instance SearchResult objSearResult = deSearch.FindOne(); //若是用戶密碼爲空 if (string.IsNullOrEmpty(TestUserPwd)) { if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0) { blRet = true; } } else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path)) { //獲取用戶名路徑對應的用戶uid int pos = objSearResult.Path.LastIndexOf('/'); string uid = objSearResult.Path.Remove(0, pos + 1); DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None); if (null != objUserEntry && objUserEntry.Properties.Count > 0) { blRet = true; } } } catch (Exception ex) { if (null != _objDirectoryEntry) { _objDirectoryEntry.Close(); } ErrorMessage = "檢測異常:"+ex.StackTrace; } return blRet; } /// <summary> /// 關閉鏈接 /// </summary> public void closeConnection() { if (null != _objDirectoryEntry) { _objDirectoryEntry.Close(); } } }
調用ui
private void btnCheck_Click(object sender, EventArgs e) { string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim()); //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; string TestUserID = txtUserName.Text; string TestUserPwd = txtPwd.Text; LDAPHelper objldap = new LDAPHelper(); string strLDAPPath = txtLDAP.Text; string strLDAPAdminName = txtLUserName.Text; string strLDAPAdminPwd = txtLPwd.Text; string strMsg = ""; bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd); if (blRet) { blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg); if (blRet) { strMsg = "檢測用戶名" + TestUserID + "和密碼" + TestUserPwd + "成功"; } else if (!blRet && string.IsNullOrEmpty(strMsg)) { strMsg = "檢測用戶名" + TestUserID + "和密碼" + TestUserPwd + "失敗"; } } this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text; MessageBox.Show(strMsg); } }
實例下載:http://download.csdn.net/detail/paolei/6740833this
LDAP是輕量目錄訪問協議,英文全稱是Lightweight Directory Access Protocol,通常都簡稱爲LDAP。它是基於X.500標準的,可是簡單多了而且能夠根據須要定製。與X.500不一樣,LDAP支持TCP/IP,這對訪問Internet是必須的。LDAP的核心規範在RFC中都有定義,全部與LDAP相關的RFC均可以在LDAPman RFC網頁中找到。spa
bool checkResult = false; try { string username = Request.Params.Get("username"); string userpwd = Request.Params.Get("userpwd"); string strLADPath = "LDAP://OU=事業部,DC=HOLD,DC=Company,DC=COM"; DirectoryEntry objEntry = new DirectoryEntry(strLADPath); objEntry.AuthenticationType = AuthenticationTypes.None; DirectorySearcher deSearch = new DirectorySearcher(objEntry); //過濾名稱是否存在 deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; deSearch.SearchScope = SearchScope.Subtree; //find the first instance SearchResult results = deSearch.FindOne(); //check username & userpwd if (null != results) { DirectoryEntry objUserEntry = new DirectoryEntry(results.Path, username, userpwd); if (null != objUserEntry && null != objUserEntry.Properties && objUserEntry.Properties.Contains("cn")) { checkResult = true; } } Response.Write("認證結果:" + checkResult.ToString()); } catch (System.Exception ex) { Response.Write("認證異常"+ex.StackTrace); Response.Write("認證結果:" + checkResult.ToString()); } private void btnCheck_Click(object sender, EventArgs e) { string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim()); //deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; string TestUserID = txtUserName.Text; string TestUserPwd = txtPwd.Text; LDAPHelper objldap = new LDAPHelper(); string strLDAPPath = txtLDAP.Text; string strLDAPAdminName = txtLUserName.Text; string strLDAPAdminPwd = txtLPwd.Text; string strMsg = ""; bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd); if (blRet) { blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg); if (blRet) { strMsg = "檢測用戶名" + TestUserID + "和密碼" + TestUserPwd + "成功"; } else if (!blRet && string.IsNullOrEmpty(strMsg)) { strMsg = "檢測用戶名" + TestUserID + "和密碼" + TestUserPwd + "失敗"; } } this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text; MessageBox.Show(strMsg); } } public class LDAPHelper { private DirectoryEntry _objDirectoryEntry; /// <summary> /// 構造函數 /// </summary> /// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param> /// <param name="authUserName">鏈接用戶名,例如"cn=root,dc=***,dc=com"</param> /// <param name="authPWD">鏈接密碼</param> public bool OpenConnection(string LADPath, string authUserName, string authPWD) { //建立一個鏈接 _objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None); if (null == _objDirectoryEntry) { return false; } else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > 0) { return true; } return false; } /// <summary> /// 檢測一個用戶和密碼是否正確 /// </summary> /// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param> /// <param name="TestUserID">testuserid</param> /// <param name="TestUserPwd">testuserpassword</param> /// <param name="ErrorMessage"></param> /// <returns></returns> public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage) { bool blRet = false; try { //建立一個檢索 DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry); //過濾名稱是否存在 deSearch.Filter =strLDAPFilter; deSearch.SearchScope = SearchScope.Subtree; //find the first instance SearchResult objSearResult = deSearch.FindOne(); //若是用戶密碼爲空 if (string.IsNullOrEmpty(TestUserPwd)) { if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > 0) { blRet = true; } } else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path)) { //獲取用戶名路徑對應的用戶uid int pos = objSearResult.Path.LastIndexOf('/'); string uid = objSearResult.Path.Remove(0, pos + 1); DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None); if (null != objUserEntry && objUserEntry.Properties.Count > 0) { blRet = true; } } } catch (Exception ex) { if (null != _objDirectoryEntry) { _objDirectoryEntry.Close(); } ErrorMessage = "檢測異常:"+ex.StackTrace; } return blRet; } /// <summary> /// 關閉鏈接 /// </summary> public void closeConnection() { if (null != _objDirectoryEntry) { _objDirectoryEntry.Close(); } } }