Zoeken…


Geverifieerde SSL LDAP-verbinding, SSL cert komt niet overeen met reverse DNS

Stel enkele constanten in voor de server en authenticatie-informatie. Uitgaande van LDAPv3, maar het is eenvoudig genoeg om dat te veranderen.

// Authentication, and the name of the server.
private const string LDAPUser = "cn=example:app:mygroup:accts,ou=Applications,dc=example,dc=com";
private readonly char[] password = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };
private const string TargetServer = "ldap.example.com";

// Specific to your company. Might start "cn=manager" instead of "ou=people", for example.
private const string CompanyDN = "ou=people,dc=example,dc=com"; 

Maak de verbinding eigenlijk met drie delen: een LdapDirectoryIdentifier (de server) en NetworkCredentials.

// Configure server and port. LDAP w/ SSL, aka LDAPS, uses port 636.
// If you don't have SSL, don't give it the SSL port. 
LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(TargetServer, 636);

// Configure network credentials (userid and password)
var secureString = new SecureString();
foreach (var character in password)
        secureString.AppendChar(character);
NetworkCredential creds = new NetworkCredential(LDAPUser, secureString);

// Actually create the connection
LdapConnection connection = new LdapConnection(identifier, creds)
{
    AuthType = AuthType.Basic, 
    SessionOptions =
    {
        ProtocolVersion = 3,
        SecureSocketLayer = true
    }
};

// Override SChannel reverse DNS lookup.
// This gets us past the "The LDAP server is unavailable." exception
// Could be 
//    connection.SessionOptions.VerifyServerCertificate += { return true; };
// but some certificate validation is probably good.
connection.SessionOptions.VerifyServerCertificate +=
    (sender, certificate) => certificate.Subject.Contains(string.Format("CN={0},", TargetServer));

Gebruik de LDAP-server, bijv. Zoek naar iemand op userid voor alle objectClass-waarden. De objectClass is aanwezig om een samengestelde zoekopdracht aan te tonen: het ampersand is de booleaanse operator "en" voor de twee queryclausules.

 SearchRequest searchRequest = new SearchRequest(
        CompanyDN, 
        string.Format((&(objectClass=*)(uid={0})), uid), 
        SearchScope.Subtree,
        null
);

// Look at your results
foreach (SearchResultEntry entry in searchResponse.Entries) {
    // do something
}

Super Simple anonieme LDAP

Uitgaande van LDAPv3, maar het is eenvoudig genoeg om dat te veranderen. Dit is een anonieme, niet-versleutelde LDAPv3 LdapConnection-creatie.

private const string TargetServer = "ldap.example.com";

Maak de verbinding eigenlijk met drie delen: een LdapDirectoryIdentifier (de server) en NetworkCredentials.

// Configure server and credentials
LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(TargetServer);
NetworkCredential creds = new NetworkCredential();
LdapConnection connection = new LdapConnection(identifier, creds)   
{
    AuthType=AuthType.Anonymous,
    SessionOptions =
    {
        ProtocolVersion = 3
    }
};

Om de verbinding te gebruiken, zou zoiets mensen met de achternaam Smith krijgen

SearchRequest searchRequest = new SearchRequest("dn=example,dn=com", "(sn=Smith)", SearchScope.Subtree,null);


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow