2014-09-11 3 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; 
    } 

가 지금은 혼자 선이 입력 한 암호 (userPwd)는 Active Directory와 일치하는지 여부를 확인하는 데 필요한 사용자 이름을 확인 :

여기 내 코드입니다. 그렇게하는 방법.

도와주세요.

+0

가능한 중복 (http://stackoverflow.com/questions/400872/active-directory- check-username-password) –

+0

.Net의 버전은 무엇입니까? – jww

+0

메신저 사용 .net 4 – Mythily

답변

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

FYI -'예외 새 예외 ("사용자 인증 오류."+ ex.Message);가 실행되지 않습니다. – Zer0

+0

나는 그것을 얻었다! 변경된 내용은 –

+0

입니다. 활성 디렉토리 개체의 (objLoginEntry) 암호를 사용하여 입력 된 암호 (userPwd)를 확인하고 있습니까? – Mythily

관련 문제