简体   繁体   English

设置 LDAP 连接 - LDAPError Invalid Credentials

[英]Setting up LDAP Connection - LDAPError Invalid Credentials

I am trying to connect to via LDAP for the first time.我第一次尝试通过 LDAP 连接。 I am just trying to simply check if a user can login.我只是想简单地检查用户是否可以登录。 After trying to connect I am getting an invalid credentials error 49 and error code 81 server is unavailable.尝试连接后,我收到无效凭据错误 49,错误代码 81 服务器不可用。 I am passing the right user credentials so this should be validating and I am able to connect via JXplorer.我正在传递正确的用户凭据,所以这应该是有效的,我可以通过 JXplorer 进行连接。 In JXplorer I have my host as ldap.my.edu port as 389 User dn as: Uid=myuser,OU=People, DC=ua,DC=edu then mypass.在 JXplorer 中,我的主机为 ldap.my.edu 端口为 389 用户 dn 为:Uid=myuser,OU=People, DC=ua,DC=edu 然后是 mypass。

I believe I am not properly translating this to LdapConnection and the.network credential.我相信我没有正确地将其转换为 LdapConnection 和 the.network 凭证。 This is my first time so any help would be very appreciated.这是我第一次,所以任何帮助将不胜感激。

            const string server = "ldap.my.edu:389/OU=People,DC=my,DC=edu";
            const string domain = "ldap.my.edu";
            string password = "mypass";
            string userName = "myuser";

            try
            {
                using (var ldapConnection = new LdapConnection(server))
                {

                    var networkCredential = new NetworkCredential(userName, password, domain);
                    ldapConnection.SessionOptions.SecureSocketLayer = true;
                    ldapConnection.AuthType = AuthType.Negotiate;
                    ldapConnection.Bind(networkCredential);
                }

If you don't have SSL ( LDAPS ) enabled on this server, which looks to be the case, then you'll want to make sure you set : 如果您确实没有在此服务器上启用SSL( LDAPS ),那么您将需要确保设置:

ldapConnection.SessionOptions.SecureSocketLayer = false

Or, you can just not set it at all - LdapConnection will default to unsecured port 389 ( LDAP ) by default, if this isn't explicitly set. 或者,您根本无法设置它-如果未明确设置,则LdapConnection将默认默认为不安全端口389( LDAP )。

An example, using the values you provided in your question, would be something like this (note that I'm applying the domain to the NetworkCredential and not the LdapConnection class itself) : 使用您在问题中提供的值的示例将是这样的(请注意,我将域应用于NetworkCredential而不是LdapConnection类本身):

// the username and password to authenticate
const string domain = "OU=People,DC=my,DC=edu";
string password = "mypass";
string userName = "myuser";

// define your connection
LdapConnection ldapConnection = new LdapConnection("ldap.my.edu:389");

try
{
   // authenticate the username and password
   using (ldapConnection)
   {
       // pass in the network creds, and the domain.
       var networkCredential = new NetworkCredential(username, password, domain);

       // if we're using unsecured port 389, set to false. If using port 636, set this to true.
       ldapConnection.SessionOptions.SecureSocketLayer = false;

       // since this is an internal application, just accept the certificate either way
       ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };

       // to force NTLM\Kerberos use AuthType.Negotiate, for non-TLS and unsecured, just use AuthType.Basic
       ldapConnection.AuthType = AuthType.Basic;

       // authenticate the user
       ldapConnection.Bind(networkCredential);
   }
   catch (LdapException ldapException)
   {
       //Authentication failed, exception will dictate why
   }
}

Try port 3268 for Global Catalog为全局目录尝试端口 3268

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM