I have this method that gets me groups from AD:

public ArrayList GetUserGroups(string sUserName)
        {
            ArrayList myItems = new ArrayList();
            UserPrincipal oUserPrincipal = GetUser(sUserName);

            PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

            foreach (Principal oResult in oPrincipalSearchResult)
            {
                myItems.Add(oResult.Name);
            }
            return myItems;
        }

But when I go to use it:

protected void AuthenticateAndLoadUserProfile()
        {
            string username;
            string password;

            username = Login1.UserName;
            password = Login1.Password;

            ADAccountManagement adAuth = new ADAccountManagement();
            try
            {
                if (true == adAuth.ValidateCredentials(username, password))
                {
                    
                    // Retrieve the user's groups
                    string groups = adAuth.GetUserGroups(username);
                    // Create the authetication ticket
                    FormsAuthenticationTicket authTicket =
                        new FormsAuthenticationTicket(1,  // version
                        username,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30),
                        false, groups);
                    // Now encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    // Create a cookie and add the encrypted ticket to the
                    // cookie as data.
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    // Add the cookie to the outgoing cookies collection.
                    Response.Cookies.Add(authCookie);
                    // Redirect the user to the originally requested page

                    Response.Redirect(FormsAuthentication.GetRedirectUrl(username, false));
                }
                else
                {
                    Login1.FailureText = "Authentication failed, check username and password.";
                }
            }
            catch (Exception ex)
            {
                Login1.FailureText = ex.Message;
            }
        }

It gives me an error of "cannot implicitly convert type 'system.collections.arraylist' to 'string'"

I have tried to change the return of the method to:

return myItems.ToString();

but then a get the reverse error on that: "cannot implicitly convert type 'string' to 'system.collections.arraylist' '"

So I am confused and I do not know what to do. Can someone please help. Thank you.

Recommended Answers

All 7 Replies

You want all of them returned in one string?

yes

using System;
using System.Collections;

namespace DW_395870
{
   class Program
   {
      static void Main(string[] args)
      {
         ArrayList arr_lst = new ArrayList()
         {
            "alpha", "bravo", "charlie"
         };

         string strABC = string.Join(",", (string[])arr_lst.ToArray("".GetType()));

         Console.WriteLine(strABC);
      }
   }
}

So I changed it to this as maybe suggested above and I still get the same error.

public ArrayList GetUserGroups(string sUserName)
        {
            ArrayList myItems = new ArrayList();
            UserPrincipal oUserPrincipal = GetUser(sUserName);

            PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

            foreach (Principal oResult in oPrincipalSearchResult)
            {
                string groupNames = string.Join(",", (string[])myItems.ToArray("".GetType()));
            }
            return myItems;
        }

You still get the error at what is shown as line 10?

Should your return type be a string and your return value be groupNames?

I understand. Try something more like this:

using System;
using System.Collections;

namespace DW_395870
{
   class Program
   {
      private static ArrayList GetUserGroups()
      {
         ArrayList arr_lst = new ArrayList()
         {
            "alpha", "bravo", "charlie"
         };

         return arr_lst;
      }

      static void Main(string[] args)
      {
         string strABC = string.Join(",", ((string[])GetUserGroups().ToArray("".GetType())));

         Console.WriteLine(strABC);
      }
   }
}

As you still need to return the ArrayList (THEN convert it).

I got it but I did it a little bit differently.

if (dsUser != null) // User is authentic
            {

                DSGroup[] userGroups = dsUser.Groups;
                StringBuilder groupNames = new StringBuilder();
                String dn;


                for (int i = 0; i < userGroups.Length; i++)
                {
                    dn = userGroups[i].Name.ToString();
                    groupNames.Append(dn).Append("|");
                }

                groups = groupNames.ToString();

Thanks for all your help thines01

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.