2011-02-02 12 views
2

私は約3年後に初めてASP.NETプロジェクトを作成しています。その間私はPython/Django、PHP、Obj-Cで作業していました。とにかく、それを直ちに選んだのですが...今のところ私を完全に殺しているものを除いて、私は顔で私を見つめなければならないと感じています:有効な資格情報が指定されている場合、LdapConnection.bind()はINVALID_CREDENTIALSをスローします

私はLDAPサーバーにバインドしようとしていますユーザーを認証する目的。ここで動作する方法は、自分の資格情報をバインドし、それを使って認証しているユーザーの識別名を見つけ、DNとそのパスワードで再度バインドします。バインドが成功した場合、パスワードは正しいものであり、ユーザーは認証できます。

ここに問題があります - 最初のバインド(固定資格情報、ユーザーとそのサブツリーを検索する能力を持つもの)は正常に動作します。検索は正常に動作します。 LDAPエラーINVALID_CREDENTIALSを使用して、2番目のバインドが失敗します。これは完全に有効な資格情報が提供されている場合でも発生します。

ここではもちろんの編集さユーザー名とパスワードを使用してコードは、私は、変数を読み出し、このコードの上にステップとのpython-LDAP経由(手動でLDAPバインドを行ってきた...

public static bool Authenticate(string username, string password) 
{ 
    bool valid = false; 

    try 
    { 
     LdapDirectoryIdentifier lid = new LdapDirectoryIdentifier("[[REDACTED]]"); 
     System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[[REDACTED]]", "[[REDACTED]]"); 

     LdapConnection lconn = new LdapConnection(lid); 
     lconn.Bind(cred); 
     SearchRequest request = new SearchRequest("[[REDACTED]]", "cn=" + username, SearchScope.Subtree, new String[] { "dn", "sn" }); 
     SearchResponse response = (SearchResponse)lconn.SendRequest(request); 

     SearchResultEntry rslt = response.Entries[0]; 
     string userdn = rslt.DistinguishedName; 
     System.Net.NetworkCredential usercred = new System.Net.NetworkCredential(userdn, password); 
     //we're already in try/catch, so if this fails we'll be booted out 
     lconn.Bind(usercred); 
     //otherwise we're all good 
     valid = true; 
    } 
    catch (LdapException e) 
    { 
     if (e.ErrorCode == 0x31) //INVALID_CREDENTIALS 
      throw (e); 
     //otherwise fall through to DB authentication 
    } 

//DB Auth goes here 

    return valid; 

} 

ですPythonコンソールで)、DNとパスワードの組み合わせがうまくいきます。何がうまくいかないの?

答えて

1

別のものからpostからです。 DNを使用して再バインドを実行する場合は、AuthType.Basicを使用する必要があります。

LdapConnectionを閉じることを忘れないように、LdapConnectionをラップしてusingを使用することもできます。

以下は、動作するはずの更新されたコードです。もちろん、基本認証が使用されているので、LDAPサーバーでSSLを有効にしたほうがよいでしょう。

public static bool Authenticate(string username, string password) 
{ 
    bool valid = false; 

    try 
    { 
     LdapDirectoryIdentifier lid = new LdapDirectoryIdentifier("[[REDACTED]]"); 
     System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[[REDACTED]]", "[[REDACTED]]"); 

     string userdn; 
     using (LdapConnection lconn = new LdapConnection(lid)) 
     { 
      lconn.Bind(cred); 
      SearchRequest request = new SearchRequest("[[REDACTED]]", "cn=" + username, SearchScope.Subtree, new String[] { "dn", "sn" }); 
      SearchResponse response = (SearchResponse)lconn.SendRequest(request); 

      SearchResultEntry rslt = response.Entries[0]; 
      userdn = rslt.DistinguishedName; 
     } 

     System.Net.NetworkCredential usercred = new System.Net.NetworkCredential(userdn, password); 
     using (LdapConnection lconn2 = new LdapConnection(lid)) 
     { 
      lconn2.AuthType = AuthType.Basic; 
      //we're already in try/catch, so if this fails we'll be booted out 
      lconn2.Bind(usercred); 
      //otherwise we're all good 
      valid = true; 
     } 
    } 
    catch (LdapException e) 
    { 
     if (e.ErrorCode == 0x31) //INVALID_CREDENTIALS 
      throw (e); 
     //otherwise fall through to DB authentication 
    } 

//DB Auth goes here 

    return valid; 

} 
+0

ahaaaa。右。私は実際にこの問題を昨日遅く解決しました...まず最初にcnとパスワードをバインドしましたか? PHPやPythonでは動作しませんが、ASP.NETでは動作します。どのように奇妙な。私は裏で起こっているAuthの魔法があると思います。 – gormster

関連する問題