SignalR hub Context.User.IsInRole(xyz) does not call CumstomRoleProvider

61 views Asked by At

Within a signalR hub method, I'm trying to check if the user authenticated is within a specific role. In plane MVC, I can call user.IsInRole and the class overriding RoleProvider's GetRolesForUser method is called.

The CustomRoleProvider is set up in the web.config with:

    <roleManager cacheRolesInCookie="false" defaultProvider="CustomRoleProvider" enabled="true">
  <providers>
    <clear />
    <add name="CustomRoleProvider" type="CustomRoleProvider" />
  </providers>
</roleManager>

When user.IsInRole is called CustomRoleProvider.GetRolesForUser(string username) is called.

Hub code

  public IEnumerable<Items> GetList()
    {
        IList<Items> result;

        var user = Context.User;
        if (user.IsInRole("Test_Role"))
            result = itemProvider.GetItems();
        else
            result = new[] {  };

        return result;
    }

CustomRoleProvider

public class CustomRoleProvider:RoleProvider
{
     public override string[] GetRolesForUser(string username)
     {
         return new[]{"Test_Role"};
     }
}

Why is this not working in signal R? user.IsInRole does not call GetRolesForUser and always returns False.

0

There are 0 answers