More in this section

Forums / General Discussions / Sitefinity 4.4 ArgumentOutOfRangeException when getting users roles

Sitefinity 4.4 ArgumentOutOfRangeException when getting users roles

2 posts, 0 answered
  1. Ryan Dailey
    Ryan Dailey avatar
    41 posts
    Registered:
    08 Mar 2010
    14 May 2012
    Link to this post
    Hello,
    This used to work in the previous version, but with the new upgrade to 4.4 it appears to not work anymore. My code is meant to retrieve all of the roles that a particular user has. Calling the method returns an IQueryable<Role>. When I check the Count on the IQueryable, I get an ArgumentOutOfRangeException. The full message below:

    The compiled query cache contains an entry for this query, but it's result converter is not producing the required type : 'Telerik.Sitefinity.Security.Model.Role, Telerik.Sitefinity.Model, Version=4.4.2117.0, Culture=neutral, PublicKeyToken=b28c218413bdf563'.
    Parameter name: targetType
    Actual value was System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.

    Here is the code I wrote:

    RoleManager appRoleManager = RoleManager.GetManager("AppRoles");
    RoleManager defaultRoleManager = RoleManager.GetManager("Default");
    UserManager userManager = UserManager.GetManager();
    Guid userKey = (Guid)Membership.GetUser().ProviderUserKey;
      
    if (HCUserInfo.IsInRole("Administrators"))
    {
        string roleNames = "";
        IQueryable<Role> appRoles = appRoleManager.GetRolesForUser(userKey);
        IQueryable<Role> defaultRoles = defaultRoleManager.GetRolesForUser(userKey);
        User user;
        if (defaultRoles != null && defaultRoles.Count<Role>() > 0) 
        {
            if (defaultRoles != null)
            {
                foreach (Role role in defaultRoles)
                {
                    roleNames += "'" + role.Name + "',";
                }
            }
        }
    }
  2. Ryan Dailey
    Ryan Dailey avatar
    41 posts
    Registered:
    08 Mar 2010
    15 May 2012
    Link to this post
    Nevermind,
    I found the problem. Turns out the user I used didn't have any roles in that particular provider after all. Fixed the problem by just reversing the if statement and placing an else clause.
2 posts, 0 answered