2016-11-14 28 views
0

私は、ユーザーの資格情報を取得するため、このコードを持っている:Kerberos認証は常に失敗し

string domain = Domain.GetComputerDomain().ToString(); 

      Console.WriteLine(domain); 
      string username = 
       new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()) 
        .Identity.Name; 

      Console.WriteLine(username); 

      Console.Write("Password: "); 
      //there are far better ways to get a hidden password this was just an easy way as it's irrelevant to the point of the application, will improve 
      string password = null; 
      while (true) 
      { 
       var key = Console.ReadKey(true); 
       if (key.Key == ConsoleKey.Enter) 
        break; 
       password += key.KeyChar; 
      } 

とKerberosで認証するためのこの方法:

private static bool ValidateCredentialsKerberos(string username, string password, string domain) 
     { 
      var credentials 
       = new NetworkCredential(username, password, domain); 

      var id = new LdapDirectoryIdentifier(domain); 

      using (var connection = new LdapConnection(id, credentials, AuthType.Kerberos)) 
      { 
       connection.SessionOptions.Sealing = true; 
       connection.SessionOptions.Signing = true; 

       try 
       { 
        connection.Bind(); 
       } 
       catch (LdapException lEx) 
       { 
        if (ERROR_LOGON_FAILURE == lEx.ErrorCode) 
        { 
         return false; 
        } 
        throw; 
       } 

      } 
      return true; 

     } 

それは常に資格情報が正しいにもかかわらず、誤った資格情報として虚偽のスロー。

Domain.net ドメイン/ユーザー パスワード

任意の考えを次のようにコンソールに出力されますか?

答えて

0

new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;はDOMAIN \ username形式でユーザー名を返しますが、LdapConnectionはユーザー名(ドメインを別のパラメーターとして既に送信しています)だけを表示することが予想されます。

Environment.UserNameを使用すると、ユーザー名だけを取得できます。

もう1つの問題は、あなたがチェックしているErrorCodeが正しくないことです。 「指定された資格が無効です」と表示されます。 DCからのメッセージ(エラーコード49)。

(ところで、あなたは新しいWindowsPrincipalを作成する必要はありませんでした、あなただけのSystem.Security.Principal.WindowsIdentity.GetCurrent().Nameを使うかもしれない)