2014-09-11 49 views
0

我通過用戶名和密碼來檢查用戶是否從Active Directory有效。如何檢查有效用戶的密碼與活動目錄匹配

這裏是我的代碼:

private bool ValidUser(string name, string userPwd) 
    { 

     string UserName = "XXXXXXXXXX"; 
     string Password = "XXXXXXXXXXXXX"; 
     DirectoryEntry objRootEntry = new DirectoryEntry("XXXXXXXX.com", UserName, Password); 
     DirectorySearcher objADSearcher = new DirectorySearcher(objRootEntry); 
     objADSearcher.Filter = ("(&(sAMAccountType=xxxxxxxxx)(samAccountName=" + name + "))"); 
     SearchResult objResult = objADSearcher.FindOne(); 
     DirectoryEntry objLoginEntry = (objResult != null) ? objResult.GetDirectoryEntry() : null;   
     if (objLoginEntry != null) 
     { 
      return true; 
     } 
     return false; 
    } 

現在檢查用戶名alone.I需要檢查輸入密碼(userPwd)是否與Active Directory相匹配。怎麼做。

請幫我一把。

+0

可能重複(http://stackoverflow.com/questions/400872/active-directory- check-username-password) – 2014-09-11 07:59:07

+0

什麼版本的.Net? – jww 2014-09-11 08:33:28

+0

即時通訊使用.net 4 – Mythily 2014-09-11 09:31:20

回答

0

//您在輸入密碼時輸入密碼就足夠了。不需要再檢查

檢查這個細節代碼

public bool ValidateUser(string domain, string username, string password,string LdapPath, out string Errmsg) 
     { 
      Errmsg = ""; 
      string domainAndUsername = domain + @"\" + username; 
      DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password); 
      try 
      { 
       // Bind to the native AdsObject to force authentication. 
       Object obj = entry.NativeObject; 
       DirectorySearcher search = new DirectorySearcher(entry); 
       search.Filter = "(SAMAccountName=" + username + ")"; 
       search.PropertiesToLoad.Add("cn"); 
       SearchResult result = search.FindOne(); 
       if (null == result) 
       { 
        return false; 
       } 
       // Update the new path to the user in the directory 
       LdapPath = result.Path; 
       string _filterAttribute = (String)result.Properties["cn"][0]; 
      } 
      catch (Exception ex) 
      { 
       Errmsg = ex.Message;     
       throw new Exception("Error authenticating user." + ex.Message); 
      } 

     } 
[活動目錄 - 檢查用戶名/密碼]的
+0

僅供參考 - '拋出新的異常(「錯誤認證用戶。」+ ex.Message);'永遠不會執行。 – Zer0 2014-09-11 06:50:49

+0

我懂了!您所做的更改是 – 2014-09-11 07:02:15

+0

是否使用活動目錄對象的(objLoginEntry)密碼檢查輸入的密碼(userPwd)? – Mythily 2014-09-11 07:54:42

相關問題