Click here to Skip to main content
15,884,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I am attempting to get the Get User Division from Active Directory. I am having problem returning the User Division after I get the Get User Group Memberships. I am only looking for the unique group membership to determine what division the User is apart of. Error Message is below the code.

C#
private string GetCurrentDomainPath()
        {
            DirectoryEntry de =
               new DirectoryEntry("LDAP://RootDSE");

            return "LDAP://" +
               de.Properties["defaultNamingContext"][0].
                   ToString();
        }

        private bool DoUserExistingInAD(string strUser)
        {

            bool result = false;

            DirectoryEntry obEntry = new DirectoryEntry(GetCurrentDomainPath());
            DirectorySearcher srch = new DirectorySearcher(obEntry, "(SAccountName=" + strUser + ")");
            SearchResult res = srch.FindOne();
            if (null != res)
            {
                result = true;
            }

            return result;

        }

        private StringCollection GetUserGroupMembership(string strUser)
        {

            StringCollection groups = new StringCollection();
            try
            {

                DirectoryEntry obEntry = new DirectoryEntry(
                GetCurrentDomainPath());
                DirectorySearcher srch = new DirectorySearcher(obEntry,
                    "(SAccountName=" + strUser + ")");
                SearchResult res = srch.FindOne();
                if (null != res)
                {
                    DirectoryEntry obUser = new DirectoryEntry(res.Path);
                    // Invoke Groups method.
                    object obGroups = obUser.Invoke("Groups");
                    foreach (object ob in (IEnumerable)obGroups)
                    {
                        // Create object for each group.
                        DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                        groups.Add(obGpEntry.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.Write(ex.Message);
            }
            return groups;
        }

  
           private ArrayList  GetUserDivision(String strUser)
           {
          // string[] userGroup;
           ArrayList userGroup = new ArrayList();
           string userGroupTemp = string.Empty;
           string _memberOf = string.Empty;
           

            try {

                foreach (string MemberOf in GetUserGroupMembership(strUser))
                {
                    if (MemberOf.Contains("Cn="))
                    {
                        //User is affiliated with "EASTREGION-W"
                        if (_memberOf.Contains("CN=EASTREGION-W"))
                        {
                            userGroupTemp = "EASTREGION-W";
                            userGroup = userGroupTemp.Split('/').ToArray();
                            return userGroup;
                        }

                        //User is affiliated with "EASTREGION-A"
                        else if (_memberOf.Contains("CN=EASTREGION-A"))
                        {
                            userGroupTemp = "EASTREGION-A";
                            userGroup = userGroupTemp.Split('/').ToArray();
                            return userGroup;
                        }

                        //User is affiliated with "EASTREGION-O"
                        else if (_memberOf.Contains("CN=EASTREGION-O"))
                        {
                            userGroupTemp = "EASTREGION-O";
                            userGroup = userGroupTemp.Split('/').ToArray();
                            return userGroup;
                        }

                        //User is affiliated with "EASTREGION-R"
                        else if (_memberOf.Contains("CN= EASTREGION-R"))
                        {
                            userGroupTemp = "EASTREGION-R";
                            userGroup = userGroupTemp.Split('/').ToArray();
                            return userGroup;
                        }
                        //User is affiliated with "EASTREGION-S"
                        else if (_memberOf.Contains("EASTREGION-S"))
                        {
                            userGroupTemp = "EASTREGION-S";
                            userGroup = userGroupTemp.Split('/').ToArray();
                            return userGroup;
                        }

                    }
                }
            }

            catch (Exception ex)
            {
                Trace.Write(ex.Message);
            } 


string[] string.Split(params char[] separator) (params char[] separator) (+5 overload(s))

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.

Error:
Cannot implicitly convert type 'string[]' to 'System.Collections.ArrayList'
Posted
Updated 13-Feb-15 12:54pm
v3

1 solution

ArrayList is sooo V1.

But why don't you simply do userGroup.Add ( userGroupTemp ) ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900