我需要验证 LDAP 中的用户/密码(在 IPA 内)。这是来自 Novell 的示例,但不起作用
System.String ldapHost = "ipa-server.ipadev.local";
System.String loginDN = "uid=tom,cn=users,cn=compat,dc=ipadev,dc=local";
System.String password = "12345678";
System.String objectDN = "cn=tim,cn=groups,cn=accounts,dc=ipadev,dc=local";
System.String testPassword = "12345678";
LdapConnection conn = new LdapConnection();
conn.SecureSocketLayer = true;
conn.UserDefinedServerCertValidationDelegate += delegate {
return true;
};
try
{
conn.Connect(ldapHost, LdapConnection.DEFAULT_SSL_PORT);
conn.Bind(loginDN, password);
LdapAttribute attr = new LdapAttribute("userPassword", testPassword);
bool correct = conn.Compare(objectDN, attr);
System.Console.Out.WriteLine(correct ? "The password is correct." : "The password is incorrect.\n");
// disconnect with the server
conn.Disconnect();
}
catch (LdapReferralException ex)
{
System.Console.Error.WriteLine ("Error: Referrals exception - " + ex.ToString());
System.Console.Error.WriteLine ("Referrals: " + ex.getReferrals ());
}
catch (LdapException e)
{
if (e.ResultCode == LdapException.NO_SUCH_OBJECT)
{
System.Console.Error.WriteLine("Error: No such entry - " + e.ToString());
}
else if (e.ResultCode == LdapException.NO_SUCH_ATTRIBUTE)
{
System.Console.Error.WriteLine("Error: No such attribute");
}
else
{
System.Console.Error.WriteLine("Error: " + e.ToString());
}
}
catch (System.IO.IOException e)
{
System.Console.Out.WriteLine("Error: " + e.ToString());
}
System.Environment.Exit(0);
如果我使用空密码 - 绑定将成功,但conn.Compare
会出现错误 -Error: LdapException: (50) Insufficient Access Rights
如果我使用普通密码(12345678),我会得到 -error "LdapReferralException: (10) Referral"
里面的 Bind
还有一个问题 - 在 loginDn 中我应该使用完整路径"uid=tom,cn=users,cn=compat,dc=ipadev,dc=local"
,但用户只拥有登录名,如何创建这个完整路径?