none
Can anybody tell me how to let "Roles.GetRolesForUser" allow a comma in its parameter? RRS feed

  • Question

  • I have been searching a solution for a long time, and so far this is still a problem for me. Whenever I use "Roles.GetRolesForUser("aa,bb")", .net framework would throw an error:"

    the parameter 'username' must not contain commas.

    "

    I know .net framework uses "SecUtility.CheckParameter" to check if a parameter contains commas, but I cannot skip or  override "SecUtility.CheckParameter", can anyone help me?

    Thanks very much!

    Saturday, May 26, 2012 11:23 PM

Answers

  • Hi, 

    Roles.GetRolesForUser, as method name describes it expect only UserName not UserNames and there is no functionality that gives you as you are looking for. So, you can implement like this, 

    public static class RoleHelper
    {
     public static string[] GetRolesByUsers(string userNames)
      {
       var roles = new List<string>();
       foreach (var list in userNames.Split(new char[] { ',' }).Select(f => Roles.GetRolesForUser()))
        {
    	roles.AddRange(list);
        }
       return roles.ToArray();
      }
    }

    then use as, 

    RoleHelper.GetRolesByUsers("a,b,c");

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Sunday, May 27, 2012 8:11 AM

All replies

  • According to the documentation on the webpage below it causes an exception.  This behavior is the way it is designed.

    http://msdn.microsoft.com/en-us/library/8h930x07.aspx


    Exception Condition
    System.ArgumentNullException

    username is null.

    System.ArgumentException

    username contains a comma (,).

    System.Configuration.Provider.ProviderException

    Role management is not enabled.



    jdweng

    Saturday, May 26, 2012 11:38 PM
  • Thanks so much. Is there any way to solve this problem? or I must give up using "Roles.GetRolesForUser"?
    Sunday, May 27, 2012 12:24 AM
  • Hi, 

    Roles.GetRolesForUser, as method name describes it expect only UserName not UserNames and there is no functionality that gives you as you are looking for. So, you can implement like this, 

    public static class RoleHelper
    {
     public static string[] GetRolesByUsers(string userNames)
      {
       var roles = new List<string>();
       foreach (var list in userNames.Split(new char[] { ',' }).Select(f => Roles.GetRolesForUser()))
        {
    	roles.AddRange(list);
        }
       return roles.ToArray();
      }
    }

    then use as, 

    RoleHelper.GetRolesByUsers("a,b,c");

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Sunday, May 27, 2012 8:11 AM
  • Hi Danny,

    One workaround can be, take an string array or list and pass the one - by - one username as parameters using foreach


    Regards, http://shwetamannjain.blogspot.com or https://shwetalodha.wordpress.com/

    Sunday, May 27, 2012 9:52 AM