为什么open OpenLDAP需要cn=username

本文关键字:username cn 需要 open OpenLDAP 为什么 | 更新日期: 2023-09-27 18:08:02

我用c#连接到OpenLDAP,当我传入我的用户名和密码时,我必须将它们传递到我的LdapConnection对象作为cn=用户名,密码。如果我只传递用户名和密码,我对Bind的调用就会失败。我为什么要这么做?在我的OpenLDAP服务器上配置错误吗?

为什么open OpenLDAP需要cn=username

这只是实现的副产品。Novell的eDirectory解决方案采用了非常相似的方法,我使用相同的Novell.Directory.Ldap代码来处理对eDirectory和OpenLDAP的绑定请求。现在很明显,用户自己在授权时不应该输入他们的整个CN—我们可以根据他们的UID对他们发出搜索:

//Setup the initial bind for the admin user
var lc = new LdapConnection();
lc.SecureSocketLayer = SSL;
lc.UserDefinedServerCertValidationDelegate += delegate { return true; };
lc.Connect(ServerName, Port);
lc.Constraints.TimeLimit = Timeout;
lc.Bind(AdminUsername, AdminPassword);

现在我只对用户进行过滤,并使用其专有名称或完整容器名称(CN)进行绑定:

//Ex. (uid=jsmith)
string filter = config.LdapAuth.LdapFilter.Replace("{{uid}}", username);
//Find the user we're trying to authorize
var lsc = lc.Search(config.LdapAuth.LdapDomain, LdapConnection.SCOPE_SUB, filter, null, false);
if (lsc.hasMore())
{
    LdapEntry nextEntry = lsc.next();
    //Check the Entries DN so we can properly bind 
    lc.Bind(nextEntry.DN, Password);
}

这是我能找到的最广泛使用的方法,到目前为止,它的效果相当好。