为什么打开OpenLDAP需要cn =用户名?

时间:2013-04-08 20:42:55

标签: c# openldap

我正在使用C#连接到OpenLDAP,当我传入我的用户名和密码时,我必须将它们作为cn = Username,Password传递给我的LdapConnection对象。如果我只是传入用户名和密码,我对Bind的调用失败了。为什么我要这样做?是否在我的OpenLDAP服务器上配置错误?

1 个答案:

答案 0 :(得分:1)

这只是实施的副产品。 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);
}

这是我能找到的最广泛使用的方法,到目前为止它运作良好。