コード例 #1
0
        public List <UserDetail> GetADList(string sDomainName, string sSearchParam = "")
        {
            const string logMethodName = "- GetADList(string sDomainName, string sSearchParam = '')- ";

            logger.Debug(logMethodName + "Begin Method");
            ADUserDetails ADUserList = new ADUserDetails();

            try
            {
                using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + sDomainName + "/OU=AMSAUsers,OU=Users,OU=Boston,DC=amsa,DC=com"))
                {
                    using (DirectorySearcher search = new DirectorySearcher(entry))
                    {
                        if (!String.IsNullOrEmpty(sSearchParam))
                        {
                            // this searches for objects that sSearchParam
                            search.Filter = (String.Format("({0}=*)", sSearchParam));
                        }

                        SearchResultCollection collectedResult = search.FindAll();

                        foreach (SearchResult temp in collectedResult)
                        {
                            UserDetail ADuser = new UserDetail();
                            if (temp.Properties["mail"].Count > 0)
                            {
                                if (!String.IsNullOrEmpty(temp.Properties["mail"][0].ToString()))
                                {
                                    ADuser.DateOfCreation = DateTime.Parse(temp.Properties["WhenCreated"][0].ToString()).ToLocalTime().ToString();
                                    //May not always be here so check first
                                    if (temp.Properties["distinguishedname"].Count > 0)
                                    {
                                        ADuser.EnvironmentName = GetEnvironmentName(temp.Properties["distinguishedname"][0].ToString());
                                    }
                                    if (temp.Properties["userPrincipalName"].Count > 0)
                                    {
                                        ADuser.UserPrincipalName = temp.Properties["userPrincipalName"][0].ToString();
                                    }
                                    if (temp.Properties["CN"].Count > 0)
                                    {
                                        ADuser.CN = temp.Properties["CN"][0].ToString();
                                    }
                                    if (temp.Properties["telephonenumber"].Count > 0)
                                    {
                                        ADuser.Phone = temp.Properties["telephonenumber"][0].ToString();
                                    }
                                    if (temp.Properties["samaccountname"].Count > 0)
                                    {
                                        ADuser.DomainID = temp.Properties["samaccountname"][0].ToString();
                                    }
                                    if (temp.Properties["title"].Count > 0)
                                    {
                                        ADuser.title = temp.Properties["title"][0].ToString();
                                    }
                                    if (temp.Properties["department"].Count > 0)
                                    {
                                        ADuser.Department = temp.Properties["department"][0].ToString();
                                    }
                                    if (temp.Properties["thumbnailphoto"].Count > 0)
                                    {
                                        ADuser.TN_Photo = temp.Properties["thumbnailphoto"][0].ToString();
                                    }

                                    ADuser.Mail     = temp.Properties["mail"][0].ToString();
                                    ADuser.UserName = temp.Properties["name"][0].ToString();
                                    ADUserList.UserDetails.Add(ADuser);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("SaltSiteADmanager.GetADList: " + ex.Message);
                ExceptionMessageException Oops = new ExceptionMessageException(ex.Message);
            }

            logger.Debug("- End Method -");
            return(ADUserList.UserDetails);
        }
コード例 #2
0
        public ADUserDetails GetUserOU(string sEmail)
        {
            const string logMethodName = "-  GetUserOU(string sEmail) - ";

            logger.Debug(logMethodName + "Begin Method");

            //get web config settings
            GetADWebConfigSettings();
            string        emailAdd = sEmail.Trim();
            int           iActiveFlag;
            bool          bisNotActive;
            ADUserDetails userDetails = new ADUserDetails();

            userDetails.EmailAddress = emailAdd;
            try
            {
                using (DirectoryEntry myLdapConnection = new DirectoryEntry(AD_LDAP_ConnectSetting.Value, AD_LDAP_UserID.Value, AD_LDAP_Password.Value, AuthenticationTypes.Secure))
                {
                    using (DirectorySearcher search = new DirectorySearcher(myLdapConnection))
                    {
                        //Searching on CN cause CN can be duplicate
                        search.Filter = ("(&(objectclass=user)(objectcategory=person)(CN=" + emailAdd + "))");
                        SearchResultCollection collectedResult = search.FindAll();

                        if (collectedResult.Count.Equals(0))
                        {
                            search.Filter   = ("(&(objectclass=user)(objectcategory=person)(mail=" + emailAdd + "))");
                            collectedResult = search.FindAll();
                        }

                        foreach (SearchResult temp in collectedResult)
                        {
                            UserDetail userDetail = new UserDetail();
                            userDetail.DateOfCreation = DateTime.Parse(temp.Properties["WhenCreated"][0].ToString()).ToLocalTime().ToString();
                            //May not always be here so check first
                            if (!String.IsNullOrEmpty(temp.Properties["distinguishedname"][0].ToString()))
                            {
                                userDetail.EnvironmentName = GetEnvironmentName(temp.Properties["distinguishedname"][0].ToString());
                            }

                            userDetail.UserName          = temp.Properties["name"][0].ToString();
                            userDetail.UserPrincipalName = temp.Properties["userPrincipalName"][0].ToString();
                            userDetail.Mail = temp.Properties["mail"][0].ToString();
                            userDetail.CN   = temp.Properties["CN"][0].ToString();

                            //check for active account
                            iActiveFlag  = (int)temp.Properties["userAccountControl"][0];
                            bisNotActive = Convert.ToBoolean(iActiveFlag & 0x0002);
                            if (bisNotActive)
                            {
                                userDetail.bisActice = false;
                            }
                            else
                            {
                                userDetail.bisActice = true;
                            }
                            userDetails.UserDetails.Add(userDetail);
                            //COV-10550 - variable not used removing.
                            //DirectoryEntry ou = temp.GetDirectoryEntry();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("SaltSiteADmanager.ADUserDetails:" + ex.Message);
                ExceptionMessageException Oops = new ExceptionMessageException(ex.Message);
                userDetails.ErrorMsg = ex.Message;
            }

            logger.Debug("- End Method -");
            return(userDetails);
        }