Ejemplo n.º 1
0
        //match the key to first name or last name
        public List <LDAPUser> GetUserListByName(string name, int rowCount)
        {
            var             ldapSettings = GetLDAPSettings();
            bool            flag         = false;
            string          uidAttribute = "uid";
            List <LDAPUser> userList     = new List <LDAPUser>();

            LdapConnection ldapConn = null;

            try
            {
                ldapConn = new LdapConnection();

                ldapConn.Connect(ldapSettings.LdapServer, ldapSettings.LdapServerPort);
                ldapConn.Bind(ldapSettings.AdminDN, ldapSettings.AdminPassword);

                var queryStr = string.Format("(|(EdsSearchFirstNm={0})(EdsSearchLastNm={1}))", name, name);

                if (ldapConn.Bound)
                {
                    //specify result's count
                    var cons = ldapConn.SearchConstraints;
                    cons.MaxResults = rowCount;
                    //---

                    LdapSearchQueue queue = ldapConn.Search(ldapSettings.SearchBase, LdapConnection.SCOPE_SUB,
                                                            queryStr,
                                                            null, false, (LdapSearchQueue)null, cons);
                    LdapMessage message;
                    while ((message = queue.getResponse()) != null)
                    {
                        LDAPUser user = new LDAPUser();
                        if (message is LdapSearchResult)
                        {
                            flag = false;
                            LdapEntry        entry               = ((LdapSearchResult)message).Entry;
                            LdapAttributeSet attributeSet        = entry.getAttributeSet();
                            System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
                            while (ienum.MoveNext())
                            {
                                LdapAttribute attribute     = (LdapAttribute)ienum.Current;
                                string        attributeName = attribute.Name;
                                string        attributeVal  = attribute.StringValue;
                                if (!Base64.isLDIFSafe(attributeVal))
                                {
                                    byte[] tbyte = SupportClass.ToByteArray(attributeVal);
                                    attributeVal = Base64.encode(SupportClass.ToSByteArray(tbyte));
                                }
                                Logger.Debug("Attributes:" + attributeName + "value:" + attributeVal);

                                if (uidAttribute.Equals(attributeName.Trim()))
                                {
                                    user.LoginId = attributeVal;
                                }
                                if (ldapSettings.MailAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Mail = attributeVal;
                                    flag      = true;
                                }
                                if (ldapSettings.NameAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Name = attributeVal;
                                    flag      = true;
                                }
                            }
                            if (flag == true)
                            {
                                if (!string.IsNullOrEmpty(user.LoginId) && !string.IsNullOrEmpty(user.Name) && !string.IsNullOrEmpty(user.Mail))
                                {
                                    userList.Add(user);
                                }
                            }
                        } //end if
                    }     //end while
                }
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserListByName failed.", ex);
                userList.Clear();
                //throw ex;
            }
            finally
            {
                if (ldapConn != null)
                {
                    ldapConn.Disconnect();
                }
            }
            return(userList);
        }
Ejemplo n.º 2
0
        public LDAPUser GetUserInfo(string userLoginId, out bool found)
        {
            var ldapSettings = GetLDAPSettings();

            found = false;
            LDAPUser user   = new LDAPUser();
            string   userId = userLoginId.ToUpper();

            user.LoginId = userLoginId;

            user.Name = userLoginId;
            //string adminDN = "uid=AdminIMS,ou=adminaccount,o=lilly,dc=com";

            //logger.Debug("adminDN:" + adminDN);
            //string adminPassword = "******";

            //string searchBase = "o=lilly,dc=com";
            //logger.Debug("searchBase:" + searchBase);

            //string mail = null;
            LdapConnection ldapConn = null;

            //logger.Debug("get mail for:" + userId);
            try
            {
                ldapConn = new LdapConnection();
                ldapConn.Connect(ldapSettings.LdapServer, ldapSettings.LdapServerPort);
                ldapConn.Bind(ldapSettings.AdminDN, ldapSettings.AdminPassword);
                if (ldapConn.Bound)
                {
                    //logger.Debug("Attributes:" + attributeName + "value:" + attributeVal);
                    LdapSearchQueue queue = ldapConn.Search(ldapSettings.SearchBase, LdapConnection.SCOPE_SUB,
                                                            "uid=" + userId,
                                                            null, false, (LdapSearchQueue)null, (LdapSearchConstraints)null);
                    LdapMessage message;

                    while ((message = queue.getResponse()) != null)
                    {
                        if (message is LdapSearchResult)
                        {
                            LdapEntry entry = ((LdapSearchResult)message).Entry;

                            LdapAttributeSet attributeSet        = entry.getAttributeSet();
                            System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
                            while (ienum.MoveNext())
                            {
                                LdapAttribute attribute     = (LdapAttribute)ienum.Current;
                                string        attributeName = attribute.Name;
                                string        attributeVal  = attribute.StringValue;
                                if (!Base64.isLDIFSafe(attributeVal))
                                {
                                    byte[] tbyte = SupportClass.ToByteArray(attributeVal);
                                    attributeVal = Base64.encode(SupportClass.ToSByteArray(tbyte));
                                }
                                Logger.Debug("Attributes:" + attributeName + "value:" + attributeVal);

                                if (ldapSettings.MailAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Mail = attributeVal;
                                    found     = true;
                                }
                                if (ldapSettings.NameAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Name = attributeVal;
                                    found     = true;
                                }
                                if (ldapSettings.CountryAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Country = attributeVal;
                                    found        = true;
                                }
                            }
                        } //end if
                    }     //end whil
                }         // end if
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserInfo failed.", ex);
                user = null;
                //throw ex;
            }
            finally
            {
                if (ldapConn != null)
                {
                    ldapConn.Disconnect();
                }
            }
            return(user);
        }
Ejemplo n.º 3
0
        public List <LDAPUser> GetUserInfoList(List <string> LoginIdList)
        {
            var             ldapSettings = GetLDAPSettings();
            bool            flag         = false;
            string          uidAttribute = "uid";
            List <LDAPUser> userList     = new List <LDAPUser>();

            LdapConnection ldapConn = null;

            try
            {
                ldapConn = new LdapConnection();
                ldapConn.Connect(ldapSettings.LdapServer, ldapSettings.LdapServerPort);
                ldapConn.Bind(ldapSettings.AdminDN, ldapSettings.AdminPassword);
                for (int i = 0; i < LoginIdList.Count; i++)
                {
                    LoginIdList[i].ToUpper();
                }
                //user.LoginId = userLoginId;
                //user.Name = userLoginId;
                StringBuilder QueryStr = new StringBuilder();
                if (LoginIdList.Count == 1)
                {
                    QueryStr.Append("uid=" + LoginIdList[0]);
                }
                else
                {
                    QueryStr.Append("(|");
                    for (int i = 0; i < LoginIdList.Count; i++)
                    {
                        QueryStr.Append("(uid=" + LoginIdList[i] + ")");
                    }
                    QueryStr.Append(")");
                }

                if (ldapConn.Bound)
                {
                    LdapSearchQueue queue = ldapConn.Search(ldapSettings.SearchBase, LdapConnection.SCOPE_SUB,
                                                            QueryStr.ToString(),
                                                            null, false, (LdapSearchQueue)null, (LdapSearchConstraints)null);
                    LdapMessage message;
                    while ((message = queue.getResponse()) != null)
                    {
                        LDAPUser user = new LDAPUser();
                        if (message is LdapSearchResult)
                        {
                            flag = false;
                            LdapEntry        entry               = ((LdapSearchResult)message).Entry;
                            LdapAttributeSet attributeSet        = entry.getAttributeSet();
                            System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
                            while (ienum.MoveNext())
                            {
                                LdapAttribute attribute     = (LdapAttribute)ienum.Current;
                                string        attributeName = attribute.Name;
                                string        attributeVal  = attribute.StringValue;
                                if (!Base64.isLDIFSafe(attributeVal))
                                {
                                    byte[] tbyte = SupportClass.ToByteArray(attributeVal);
                                    attributeVal = Base64.encode(SupportClass.ToSByteArray(tbyte));
                                }
                                Logger.Debug("Attributes:" + attributeName + "value:" + attributeVal);

                                if (uidAttribute.Equals(attributeName.Trim()))
                                {
                                    user.LoginId = attributeVal;
                                }
                                if (ldapSettings.MailAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Mail = attributeVal;
                                    flag      = true;
                                }
                                if (ldapSettings.NameAttribute.Equals(attributeName.Trim()))
                                {
                                    user.Name = attributeVal;
                                    flag      = true;
                                }
                            }
                            if (flag == true)
                            {
                                if (!string.IsNullOrEmpty(user.LoginId) && !string.IsNullOrEmpty(user.Name) && !string.IsNullOrEmpty(user.Mail))
                                {
                                    userList.Add(user);
                                }
                            }
                        } //end if
                    }     //end while
                }
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserInfoList failed.", ex);
                userList.Clear();
                //throw ex;
            }
            finally
            {
                if (ldapConn != null)
                {
                    ldapConn.Disconnect();
                }
            }
            return(userList);
        }