public User Login(string userName, string password)
        {
            User user = new User();


            using (var cn = new Novell.Directory.Ldap.LdapConnection())
            {
                cn.Connect(config.Path, config.Port);

                try
                {
                    cn.Bind(config.UserDomainName + "\\" + userName, password);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Failed login attempt for user " + userName);
                    user = null;
                    return(user);
                }

                string filter = "sAMAccountname=" + userName;

                string baseStr = "OU=BLS,DC=blacklanternsecurity,DC=com";

                LdapSearchResults result = (LdapSearchResults)cn.Search(baseStr, LdapConnection.ScopeSub, filter, null, false);

                LdapEntry entry = null;
                try
                {
                    entry = result.First();
                }
                catch (LdapException e)
                {
                    Console.WriteLine("Error: " + e.LdapErrorMessage);
                }

                LdapAttributeSet attributeSet = entry.GetAttributeSet();

                user.DisplayName = attributeSet.GetAttribute("displayName").StringValue;
                user.GivenName   = attributeSet.GetAttribute("givenName").StringValue;
                user.UserName    = userName;

                return(user);
            }
        }
Exemple #2
0
        public bool SearchUser(Models.Environment enviro, ref User user)
        {
            bool rval = false;

            try
            {
                string server = "", bindPwd = "";
                switch (enviro)
                {
                case Models.Environment.QA:
                    server  = GetStringValue("S_ldap_ED_server_QA");
                    bindPwd = GetStringValue("S_ldap_ED_bindPwd_QA");
                    break;

                case Models.Environment.Dev:
                    server  = GetStringValue("S_ldap_ED_server_Dev");
                    bindPwd = GetStringValue("S_ldap_ED_bindPwd_Dev");
                    break;

                default:     // Prod
                    server  = GetStringValue("S_ldap_ED_server");
                    bindPwd = GetStringValue("S_ldap_ED_bindPwd");
                    break;
                }
                ILdapConnection ldapConn = new LdapConnection()
                {
                    SecureSocketLayer = GetBoolValue("S_ldap_ED_isSSL")
                };
                ldapConn.Connect(server, GetIntValue("S_ldap_ED_port"));
                ldapConn.Bind(GetStringValue("S_ldap_ED_bindName"), bindPwd);

                string            userSearch = String.Format(GetStringValue("S_ldap_ED_userNameFrmt"), user.login_id);
                string            baseSearch = ""; // "ou=People,o=eaton.com";
                LdapSearchResults search     = ldapConn.Search(baseSearch, LdapConnection.SCOPE_SUB, userSearch, null, false);
                if (search != null)                // && search.Count > 0)
                {
                    LdapEntry le = search.First <LdapEntry>();
                    if (le != null)
                    {
                        string           name = le.DN;
                        LdapAttributeSet set  = le.getAttributeSet();
                        if (set != null)
                        {
                            IEnumerator ienum = set.GetEnumerator();
                            while (ienum.MoveNext())
                            {
                                LdapAttribute attribute     = (LdapAttribute)ienum.Current;
                                string        attributeName = attribute.Name;
                                string        attributeVal  = attribute.StringValue;

                                if (String.Compare(attributeName, GetStringValue("S_ED_FirstName"), true) == 0)
                                {
                                    user.first_name = attributeVal;
                                }
                                else if (String.Compare(attributeName, GetStringValue("S_ED_LastName"), true) == 0)
                                {
                                    user.last_name = attributeVal;
                                }
                                else if (String.Compare(attributeName, GetStringValue("S_ED_Email"), true) == 0)
                                {
                                    user.email = attributeVal;
                                }
                                else
                                {
                                    user.attributes.Add(attributeName, attributeVal);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Exception in SearchUser(), details: " + ex.ToString());
                throw ex;
            }
            return(rval);
        }