通过 LDAP 进行 Kerberos 身份验证

2024-02-03

我正在开发控制台应用程序,它使用 ldap DirectoryServices.Protocols 从活动目录中获取用户数据。目前,我可以使用 SSL、TLS 和简单连接(既不是 SSL 也不是 TLS)的基本身份验证来获取数据。但现在我想通过 SSL、TLS 和简单连接使用 kerberos 身份验证来获取数据。我目前正在使用以下代码。

LdapDirectoryIdentifier ldap_id = new LdapDirectoryIdentifier(
                                            host, 
                                            Int32.Parse(port), 
                                            true, 
                                            false);
LdapConnection con = new LdapConnection(ldap_id);

con.AuthType = AuthType.Kerberos;
con.SessionOptions.Sealing = true;
con.SessionOptions.Signing = true;
con.SessionOptions.ProtocolVersion = 3;

con.Bind();

这给了我错误“ldap 服务器不可用”。有人可以建议上面的代码有什么问题吗?另外,如果我需要在服务器和客户端上进行 kerberos 身份验证的任何设置,请告诉我。当我传递基本身份验证时,是否需要传递下面给出的网络凭据?

LdapDirectoryIdentifier ldapIdentifier = new LdapDirectoryIdentifier(
                                            host, 
                                            Int32.Parse(port), 
                                            true, 
                                            false);
NetworkCredential credential = new NetworkCredential(username, password);
LdapConnection con = new LdapConnection(ldapIdentifier, credential, AuthType.Kerberos);    
con.SessionOptions.Sealing = true;
con.SessionOptions.Signing = true;
con.SessionOptions.ProtocolVersion = 3;
con.Bind();

下面的代码适用于基于 SSL、TLS 的基本身份验证、Kerberos 身份验证以及基于 LDAP 的简单身份验证(既不是 SSL 也不是 TLS 连接)。

注意:传递给 NetworkCredential 的 connectionAccountName 应该是用户主体名称。您可以通过检查 Active Directory 用户的 AttributeEditor 部分中的属性 userPrincipleName 值来检查用户的主体名称,ssl 的端口为 636,其他端口为 389。

var networkCredential = new NetworkCredential(connectionAccountName, connectionAccountPassword);
LdapDirectoryIdentifier ldapDirectoryIdentifier = null;

switch (connectionType)
{
    case LDAPConnectionType.SSL:
                ldapDirectoryIdentifier = new LdapDirectoryIdentifier(ldapServerName, Convert.ToInt16(LDAPPorts.SSL));
                ldapConnection = new LdapConnection(ldapDirectoryIdentifier, networkCredential, authType);
                ldapConnection.SessionOptions.ProtocolVersion = 3;
                ldapConnection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallback);
                ldapConnection.SessionOptions.SecureSocketLayer = true;

                break;

    case LDAPConnectionType.TLS:
                ldapDirectoryIdentifier = new LdapDirectoryIdentifier(ldapServerName, Convert.ToInt16(LDAPPorts.Default));
                ldapConnection = new LdapConnection(ldapDirectoryIdentifier, networkCredential, authType);
                ldapConnection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallback);
                ldapConnection.SessionOptions.StartTransportLayerSecurity(null);

                break;

    default:
                ldapDirectoryIdentifier = new LdapDirectoryIdentifier(ldapServerName, Convert.ToInt16(LDAPPorts.Default));
                ldapConnection = new LdapConnection(ldapDirectoryIdentifier, networkCredential, authType);

                break;
}

ldapConnection.Bind();

Thanks

乌梅什·塔亚德

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 LDAP 进行 Kerberos 身份验证 的相关文章

随机推荐