Exemplo n.º 1
0
        public LdapUser Authenticate(string userName, string password)
        {
            userName = _sanitizeUsername(userName);
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                throw new LdapAuthenticationException("Empty user or password");
            }
            using (var ldap = new LdapConnection())
            {
                try
                {
                    ldap.Connect(this.HostName, this.Port);
                    ldap.Bind(null, null); // anonymous bind
                }
                catch (LdapException e)
                {
                    throw new Exception("Error connecting to LDAP server");
                }

                var entry = GetEntry(ldap, userName);
//#if DEBUG
//                return LdapUser.FromEntry(entry);
//#endif
                var dn = entry.DN;
                try
                {
                    ldap.Bind(dn, password);
                    return(LdapUser.FromEntry(entry));
                }
                catch (LdapException)
                {
                    throw new LdapAuthenticationException("Invalid user or password");
                }
            }
        }
Exemplo n.º 2
0
        public static LdapUser FromEntry(LdapEntry entry)
        {
            var attributes = entry.getAttributeSet();
            var iter       = attributes.GetEnumerator();
            var user       = new LdapUser();

            while (iter.MoveNext())
            {
                var attribute     = (LdapAttribute)iter.Current;
                var attributeName = attribute.Name.ToUpper();
                var values        = attribute.StringValueArray;
                switch (attributeName)
                {
                case "UID":
                    user.Uid = values[0]; break;

                case "CN":
                    user.Name = values[0]; break;

                case "MAIL":
                    user.Email = values[0]; break;

                case "NIP":
                    user.Nip = values[0]; break;

                case "NRP":
                    user.Nim = values[0]; break;

                case "OBJECTCLASS":
                    foreach (var v in values)
                    {
                        switch (v)
                        {
                        case "student":
                            user.TipeAkun = TipeAkunLdap.Student;
                            break;

                        case "tendik":
                            user.TipeAkun = TipeAkunLdap.Tendik;
                            break;
                            //case "dosen":
                            //    user.TipeAkun = TipeAkunLdap.Dosen;
                            //    break;
                        }
                    }
                    break;
                }
            }

            return(user);
        }