验证密码Mono + IPA(LDAP)+ rhel

时间:2013-09-04 08:31:50

标签: c# mono ldap rhel

我需要验证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),我会在Bind中获得 - error "LdapReferralException: (10) Referral"

还有一个问题 - 在loginDn中我应该使用像"uid=tom,cn=users,cn=compat,dc=ipadev,dc=local"这样的完整路径,但是用户只拥有登录信息,如何创建这条完整路径?

1 个答案:

答案 0 :(得分:0)

这不是你在LDAP中的表现。我的想法是将作为用户与该密码连接起来,看看是否成功。