LDAP 连接响应服务器不可用
LDAP connection response server unavailable
我正在尝试使用各种代码片段对 LDAP 进行身份验证,但始终无法获得 ldap 服务器。
当我尝试连接 LDAP 浏览器时使用相同的连接,但它起作用了。
http://btechintegrator.com/index.php/2020/01/22/free-online-cloud-ldap/
知道我应该使用什么特定的 C# 代码片段来连接和验证
尝试关注
try
{
LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier("LDAP://www.btechldap.com", 1389));
con.SessionOptions.SecureSocketLayer = true;
con.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallBack);
con.Credential = new NetworkCredential("cn=readonlybind,ou=admins,dc=btechsample,dc=com", "btechpass");
con.AuthType = AuthType.Basic;
con.Bind();
}
catch (Exception ex)
{
}
try
{
string username = "developer";
string password = "developer_pass";
string filter = "(&(&(objectclass=user)(objectcategory=person))" +
"sAMAccountName=username)";
NetworkCredential credentials = new NetworkCredential(username, password);
LdapDirectoryIdentifier directoryIdentifier =
new LdapDirectoryIdentifier("LDAP://shihadeh.intern/cn=developer,ou=admins,dc=shihadeh,dc=intern", 389, true, false);
using (LdapConnection connection =
new LdapConnection(directoryIdentifier, credentials, AuthType.Basic))
{
connection.Timeout = new TimeSpan(0, 0, 90);
connection.SessionOptions.ProtocolVersion = 3;
connection.Bind();
//SearchRequest search =
// new SearchRequest(username, filter, System.DirectoryServices.Protocols.SearchScope.Base, "mail");
//SearchResponse response = connection.SendRequest(search) as SearchResponse;
//foreach (SearchResultEntry entry in response.Entries)
//{
// Console.WriteLine(entry.Attributes["mail"][0]);
//}
}
}
catch (Exception ex)
{
}
我可以通过 Java 以下代码进行连接。需要等效的 .net one
public static void main(String[] args) {
String url = "ldap://localhost:389";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=developer,dc=shihadeh,dc=intern");
env.put(Context.SECURITY_CREDENTIALS, "developer_pass");
try {
DirContext ctx = new InitialDirContext(env);
System.out.println("connected");
System.out.println(ctx.getEnvironment());
// do something useful with the context...
ctx.close();
} catch (AuthenticationNotSupportedException ex) {
System.out.println("The authentication is not supported by the server");
} catch (AuthenticationException ex) {
System.out.println("incorrect password or username");
} catch (NamingException ex) {
System.out.println("error when trying to create the context");
}
System.console().readLine();
}
根据 documentation,您正在使用的 LdapDirectoryIdentifier
构造方法需要一组 LDAP 服务器 hostnames 或 ip 地址,不是 ldap url 语法。
试试这个:
string[] servers = {"shihadeh.intern"}
LdapDirectoryIdentifier directoryIdentifier =
new LdapDirectoryIdentifier(servers, 389, true, false);
我的错误是服务器路径地址中的ldap://
以下代码有效:
try
{
// don't add LDAP://, the protocol is already known ...
LdapConnection ldapConnection = new LdapConnection("127.0.0.1:389");
// notice we don't use the domain here
var networkCredential = new NetworkCredential(
"cn=developer,dc=shihadeh,dc=intern",
"developer_pass");
// Apache Directory Server uses LDAPv3
ldapConnection.SessionOptions.ProtocolVersion = 3;
// 10389 is the plain port, no ssl needed
//ldapConnection.SessionOptions.SecureSocketLayer = true;
// ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
// let's not negotiate, only Basic is supported
ldapConnection.AuthType = AuthType.Basic;
ldapConnection.Bind(networkCredential);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
我正在尝试使用各种代码片段对 LDAP 进行身份验证,但始终无法获得 ldap 服务器。 当我尝试连接 LDAP 浏览器时使用相同的连接,但它起作用了。 http://btechintegrator.com/index.php/2020/01/22/free-online-cloud-ldap/
知道我应该使用什么特定的 C# 代码片段来连接和验证
尝试关注
try
{
LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier("LDAP://www.btechldap.com", 1389));
con.SessionOptions.SecureSocketLayer = true;
con.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallBack);
con.Credential = new NetworkCredential("cn=readonlybind,ou=admins,dc=btechsample,dc=com", "btechpass");
con.AuthType = AuthType.Basic;
con.Bind();
}
catch (Exception ex)
{
}
try
{
string username = "developer";
string password = "developer_pass";
string filter = "(&(&(objectclass=user)(objectcategory=person))" +
"sAMAccountName=username)";
NetworkCredential credentials = new NetworkCredential(username, password);
LdapDirectoryIdentifier directoryIdentifier =
new LdapDirectoryIdentifier("LDAP://shihadeh.intern/cn=developer,ou=admins,dc=shihadeh,dc=intern", 389, true, false);
using (LdapConnection connection =
new LdapConnection(directoryIdentifier, credentials, AuthType.Basic))
{
connection.Timeout = new TimeSpan(0, 0, 90);
connection.SessionOptions.ProtocolVersion = 3;
connection.Bind();
//SearchRequest search =
// new SearchRequest(username, filter, System.DirectoryServices.Protocols.SearchScope.Base, "mail");
//SearchResponse response = connection.SendRequest(search) as SearchResponse;
//foreach (SearchResultEntry entry in response.Entries)
//{
// Console.WriteLine(entry.Attributes["mail"][0]);
//}
}
}
catch (Exception ex)
{
}
我可以通过 Java 以下代码进行连接。需要等效的 .net one
public static void main(String[] args) {
String url = "ldap://localhost:389";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=developer,dc=shihadeh,dc=intern");
env.put(Context.SECURITY_CREDENTIALS, "developer_pass");
try {
DirContext ctx = new InitialDirContext(env);
System.out.println("connected");
System.out.println(ctx.getEnvironment());
// do something useful with the context...
ctx.close();
} catch (AuthenticationNotSupportedException ex) {
System.out.println("The authentication is not supported by the server");
} catch (AuthenticationException ex) {
System.out.println("incorrect password or username");
} catch (NamingException ex) {
System.out.println("error when trying to create the context");
}
System.console().readLine();
}
根据 documentation,您正在使用的 LdapDirectoryIdentifier
构造方法需要一组 LDAP 服务器 hostnames 或 ip 地址,不是 ldap url 语法。
试试这个:
string[] servers = {"shihadeh.intern"}
LdapDirectoryIdentifier directoryIdentifier =
new LdapDirectoryIdentifier(servers, 389, true, false);
我的错误是服务器路径地址中的ldap://
以下代码有效:
try
{
// don't add LDAP://, the protocol is already known ...
LdapConnection ldapConnection = new LdapConnection("127.0.0.1:389");
// notice we don't use the domain here
var networkCredential = new NetworkCredential(
"cn=developer,dc=shihadeh,dc=intern",
"developer_pass");
// Apache Directory Server uses LDAPv3
ldapConnection.SessionOptions.ProtocolVersion = 3;
// 10389 is the plain port, no ssl needed
//ldapConnection.SessionOptions.SecureSocketLayer = true;
// ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
// let's not negotiate, only Basic is supported
ldapConnection.AuthType = AuthType.Basic;
ldapConnection.Bind(networkCredential);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}