Skip to content

Instantly share code, notes, and snippets.

@xjoker
Created November 21, 2021 01:58
Show Gist options
  • Save xjoker/d4184817acfbed94c6e53a52a1e899f6 to your computer and use it in GitHub Desktop.
Save xjoker/d4184817acfbed94c6e53a52a1e899f6 to your computer and use it in GitHub Desktop.
LDAP SSL 连接
using System;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using Novell.Directory.Ldap;
namespace LDAPTest
{
class Program
{
static void Main(string[] args)
{
var ADAddress = "********";
Console.WriteLine(Environment.OSVersion.ToString());
var options = new LdapConnectionOptions()
.UseSsl()
.ConfigureRemoteCertificateValidationCallback(SslValidationDelegate)
.ConfigureSslProtocols(SslProtocols.Tls11);
var ldapConn = new LdapConnection(options);
ldapConn.Connect(ADAddress,636);
ldapConn.Bind("cn=administrator,cn=users,dc=********,dc=********,dc=********", "********");
string authInfo = ldapConn.AuthenticationDn;
var searchResult=ldapConn.Search("dc=********,dc=********,dc=********", LdapConnection.ScopeSub, $"(cn=administrator)", null, false);
while (searchResult.HasMore())
{
try
{
var tempEntity = searchResult.Next();
Console.WriteLine(tempEntity.ToString());
}
catch (Exception e)
{
Console.WriteLine(e);
continue;
}
}
Console.WriteLine("Hello World!");
}
private static bool SslValidationDelegate(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment