0

始める前に、LDAPS接続で不明なエラー(0x80005000)にアクセスしてコードを変更しましたが、問題は解決しましたが、不思議なことに戻ってきたようです。

これが良いものです:

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

    try
    {
        LdapConnection con = new LdapConnection(
            new LdapDirectoryIdentifier(Host, Port));
        if (IsSSL)
        {
            con.SessionOptions.SecureSocketLayer = true;
            con.SessionOptions.VerifyServerCertificate = ServerCallback;
        }
        con.Credential = new NetworkCredential(username, password);
        con.AuthType = AuthType.Basic;
        con.Bind();
        authentic = true;
    }
    catch (LdapException)
    {
        return false;
    }
    catch (DirectoryServicesCOMException)
    { }
    return authentic;
}

public static bool IsSSL
{
    get
    {
        return ConnectionString.ToLower().Contains("ldaps");
    }
}

public static string ConnectionString
{
    get
    {
        if (string.IsNullOrEmpty(_connectionString))
            _connectionString = CompleteConfiguration.GetLDAPConnectionString();

        return _connectionString;
    }
    set { _connectionString = value; }
}

public static int Port
{
    get
    {
        var x = new Uri(ConnectionString);
        int port = 0;
        if (x.Port != -1)
        {
            port = x.Port;
        }
        else
        {
            port = x.OriginalString.ToLower().Contains("ldaps")
                       ? 636
                       : 389;
        }
        return port;
    }
}

public static string Host
{
    get
    {
        var x = new Uri(ConnectionString);
        return x.Host;
    }
}

private static bool ServerCallback(LdapConnection connection, X509Certificate certificate)
{
    return true;
}

悪い点は次のとおりです。アプリケーションに対して認証しようとすると、次のエラーが発生します。正確には、これはcon.Bind()行によってトリガーされます。

[COMException(0x80005000):不明なエラー(0x80005000)] System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)+378094 System.DirectoryServices.DirectoryEntry.Bind()+36 System.DirectoryServices.DirectoryEntry.get_NativeObject()+31 Complete.Authentication .GCAuthentication.Authenticate(String username、String password、String domain)in c:\ Builds \ 6 \ Idealink.Open.Pancanal \ Panama Canal \ Sources \ Idealink.Open \ Complete.Authentication \ GCAuthentication.cs:27Complete.Authentication。 AuthenticationFactory.ValidateUserLdap(String username、String password、String domain、Boolean isValid、String usernameWithDomain)in c:\ Builds \ 6 \ Idealink.Open.Pancanal \ Panama Canal \ Sources \ Idealink.Open \ Complete.Authentication \ AuthenticationFactory.cs: 93

一部のユーザーアカウントは機能し、他のユーザーアカウントは機能しないように見えるため、非常に混乱します。ただし、上記のコードを分離されたテスト環境に配置すると、使用するアカウントに関係なく、毎回成功します。ASP.NETとIISを使用してWindows2008R2サーバーに戻すと、上記のように失敗します。ただし、失敗は一貫しています。アカウントは一貫して失敗または成功します。その観点からは、ランダム性はありません。

LDAPサーバーにはLDAPではなくLDAPSを使用してアクセスする必要があります。そのため、DirectoryEntryオブジェクトを使用できません。LDAPサーバーはクライアントによって制御されるため、再構成または変更することはできません。Webフォームでユーザー名/パスワードを取得し、LDAPサーバーでBINDを使用して資格情報を確認するだけです。

.NET 3.5を使用しており、現時点ではアップグレードできません。そのため、主な提案や議論がアップグレードである場合は、貢献を控えてください。

ありがとう、あなたが助けることができることを願っています

4

1 に答える 1

2

このようなものはあなたのために働きますか..?

const string Domain = "ServerAddress:389";
const string constrParts = @"OU=Users,DC=domain,DC=com";
const string Username = @"karell";
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, constrParts);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  username);

これは素晴らしい参考資料と例のための本当に良いサイトです DirectoryServicesDirectoryEntry

SSLを介した接続の場合、次のようなことができます

const int ldapInvalidCredentialsError = 0x31;
const string server = "your_domain.com:636";
const string domain = "your_domain.com";

try
{
    using (var ldapSSLConn = new LdapConnection(server))
    {
        var networkCredential = new NetworkCredential(username, password, domain);
        ldapSSLConn.SessionOptions.SecureSocketLayer = true;
        ldapSSLConn.AuthType = AuthType.Negotiate;
        ldapSSLConn.Bind(networkCredential);
    }

    // If the bind succeeds, the credentials are valid
    return true;
}
catch (LdapException ldapEx)
{
    // Invalid credentials a specific error code
    if (ldapEx.ErrorCode.Equals(ldapInvalidCredentialsError))
    {
        return false;
    }

    throw;
}

無効なLDAPエラーコードのMSDNリスト

于 2013-01-08T16:04:59.360 に答える