認証

2016-08-19 1 views
1

私は他の誰かによって作られたこの方法を持っているし、それは完全に正常に動作認証

問題は、私は何かのためにドメインを変更した場合でも、既存のない、検索者がまだそのための結果を見つけることですユーザ名が間違ったドメインであっても

public bool Validarcredenciales(string domain, ControlarSesiones objeto)//Metodo que valida si las credenciales son correctas. 
{ 
    string username = objeto.Usuario; 
    string pwd = objeto.Clave; 
    String domainAndUsername = domain + @"\" + username; 
    DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); 

    try 
    { //Bind to the native AdsObject to force authentication. 
     //Object obj = entry.NativeObject; 

     DirectorySearcher search = new DirectorySearcher(entry) { Filter = "(SAMAccountName=" + username + ")" }; 

     search.PropertiesToLoad.Add("cn"); 
     SearchResult result = search.FindOne(); 

     if (null == result) 
     { 
      MensajeError = Resources.ResourcesETB.ErrorCredenciales; 
      return false; 
     } 

     //Update the new path to the user in the directory. 
     _path = result.Path; 
     FilterAttribute = (string)result.Properties["cn"][0]; 
    } 
    catch (Exception ex) 
    { 
     MensajeError = Resources.ResourcesETB.ErrorCredenciales; 
     return false; 
    } 

    return true; 
} 

答えて

3

LDAP接続では奇妙な認証ロジックが使用されます。 LDAP接続が "Domain \ User"形式を使用して作成され、ドメインが存在する場合、ドメインコントローラは指定された資格情報を使用して接続しようとします。

ただし、指定されたドメインが存在しない場合、ドメインコントローラはドメイン部分を削除し、ローカルドメイン(DCのローカル)を使用してユーザーを認証しようとします。

コードでは、ドメイン名はドメインへの接続を開始するためにのみ使用されます(DirectoryEntryオブジェクトの作成)。したがって、上記で説明したように、ドメインコントローラは間違ったドメインを削除し、ユーザーを正しく認証します。

、ユーザーが指定したドメインに実際にあることを確認したい場合は、あなたがLDAP://cn=user,cn=Users,dc=yourDomain,dc=comのようなもので、ユーザーの識別名を解析し、またはin this answerが説明したように、NTAccountオブジェクトを取得するためにSIDを解析することができ、次のいずれか

DirectorySearcher search = new DirectorySearcher(entry) { Filter = "(SAMAccountName=" + username + ")" }; 

search.PropertiesToLoad.Add("cn"); 
search.PropertiesToLoad.Add("objectsid"); 
SearchResult result = search.FindOne(); 

ResultPropertyValueCollection propertyValues = result.Properties["objectsid"]; 
byte[] objectsid = (byte[])propertyValues[0]; 

SecurityIdentifier sid = new SecurityIdentifier(sid, 0) 

NTAccount account = (NTAccount) sid.Translate(typeof (NTAccount)); 
account.ToString(); // This gives the DOMAIN\User format for the account