Ejemplo n.º 1
0
 public LdapEventArgs(LdapClient client)
 {
     this.fClient = client;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Thread safe way to execute ldap requests
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public LookupResults Login(String username, String password)
        {
            if (String.IsNullOrEmpty(fUserSearchBase))
            {
                throw new LdapException("UserSearchBase is not set");
            }

            if (fSearchGroups && String.IsNullOrEmpty(fGroupSearchBase))
            {
                throw new LdapException("GroupSearchBase is not set");
            }

            if (String.IsNullOrEmpty(fLookupDN))
            {
                throw new LdapException("LookupDN is not set");
            }

            if (String.IsNullOrEmpty(fLookupPassword))
            {
                throw new LdapException("LookupPassword is not set");
            }

            if (String.IsNullOrEmpty(fHostname))
            {
                throw new LdapException("HostName is not set");
            }

            if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
            {
                return(null);
            }

            using (LdapClient lClient = new LdapClient())
            {
#if FULLFRAMEWORK
                lClient.SslOptions  = fSslOptions;
                lClient.UseStartTLS = fUseStartTLS;
#endif
                lClient.HostName = fHostname;
                lClient.Port     = fPort;

                if (BeforeConnect != null)
                {
                    BeforeConnect(this, new LdapEventArgs(lClient));
                }

                lClient.Open();

                if (AfterConnect != null)
                {
                    AfterConnect(this, new LdapEventArgs(lClient));
                }

                lClient.BindDN       = fLookupDN;
                lClient.BindPassword = fLookupPassword;
                lClient.Bind();

                LookupResults lResult = new LookupResults();

                foreach (LdapObject obj in lClient.Search(
                             fUserSearchBase,
                             fSearchSubTree ? LdapClient.SearchScope.FullSubtree : LdapClient.SearchScope.SingleLevel,
                             LdapClient.AliasDereferencing.Always,
                             0, 0, false, fUserFilter,
                             new String[] { fUserNameField }))
                {
                    if (obj.Attributes.GetStringAttribute(fUserNameField) == username)
                    {
                        lResult.DN       = obj.DN;
                        lResult.Username = obj.Attributes.GetStringAttribute(fUserNameField);
                        break;
                    }
                }

                if (lResult.DN == null)
                {
                    return(null);
                }

                foreach (LdapObject obj in lClient.Search(lResult.DN, LdapClient.SearchScope.BaseObject, LdapClient.AliasDereferencing.Always, 0, 0, false, null, null))
                {
                    lResult.UserObject = obj;
                    break;
                }

                if (fSearchGroups)
                {
                    String s = "(&" + fGroupFilter + "(" + fGroupMemberField + "=" + lResult.DN + "))";
                    //(&(objectClass=groupOfNames)(member=uid=ck,ou=users,dc=remobjects,dc=com))
                    //String s = "(objectClass=groupOfNames)";
                    //String s = "(member=uid=ck,ou=users,dc=remobjects,dc=com)";
                    foreach (LdapObject obj in
                             lClient.Search(fGroupSearchBase, fSearchSubTree ? LdapClient.SearchScope.FullSubtree : LdapClient.SearchScope.SingleLevel,
                                            LdapClient.AliasDereferencing.Always,
                                            0, 0, false, s, new String[] { fGroupNameField, fGroupMemberField }))
                    {
                        if (fStripGroupBaseDN)
                        {
                            lResult.GroupMembership.Add(StripGroupBase(obj.DN));
                        }
                        else
                        {
                            lResult.GroupMembership.Add(obj.DN);
                        }
                    }
                }

                lClient.BindDN       = lResult.DN;
                lClient.BindPassword = password;
                try
                {
                    lClient.Bind();
                }
                catch (LdapException)
                {
                    lResult = null;
                }

                lClient.Unbind();

                if (Disconnected != null)
                {
                    Disconnected(this, new LdapEventArgs(lClient));
                }

                return(lResult);
            }
        }