コード例 #1
0
        public static bool TestUser(LdapSettings ldapSettings, string uid, string password)
        {
            bool result = false;

            LdapUser testUser = LdapLogin(ldapSettings, uid, password);

            if (testUser != null)
            {
                result = true;
            }

            return(result);
        }
コード例 #2
0
        public static LdapUser LdapLogin(LdapSettings ldapSettings, string uid, string password)
        {
            LdapConnection conn = null;

            try
            {
                conn = GetConnection(ldapSettings);
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                log.Error("couldn't connect to ldap server ", ex);
            }

            bool     success = false;
            LdapUser user    = null;

            if ((conn != null) && (conn.Connected))
            {
                LdapEntry entry = null;

                try
                {
                    // open ldap uses uid
                    if (ldapSettings.UserDNKey == "uid")
                    {
                        entry = GetOneUserEntry(conn, ldapSettings, uid);
                        if (entry != null)
                        {
                            LdapConnection authConn = GetConnection(ldapSettings);
                            authConn.Bind(entry.DN, password);
                            authConn.Disconnect();
                            success = true;
                        }
                    }
                    else
                    {
                        // Active Directory uses CN

                        // might need this if other Ldap Servers besides Active Directory use CN
                        //conn.Bind(
                        //    ldapSettings.UserDNKey + "=" + uid + "," + ldapSettings.RootDN, password);


                        // this works with Active Directory
                        conn.Bind(uid + "@" + ldapSettings.Domain, password);
                        success = conn.Bound;
                        entry   = GetOneUserEntry(conn, ldapSettings, uid);
                    }
                }
                catch (Novell.Directory.Ldap.LdapException ex)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("login failure", ex);
                    }
                    success = false;
                }

                if (success)
                {
                    if (entry != null)
                    {
                        user = new LdapUser(entry);
                    }
                    else
                    {
                        user = new LdapUser(ldapSettings, uid);
                    }
                }

                conn.Disconnect();
            }

            return(user);
        }