Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:31:40+00:00 2026-06-15T20:31:40+00:00

My MVC application makes use of a User’s Role in multiple places during individual

  • 0

My MVC application makes use of a User’s Role in multiple places during individual page requests. My question is whether the default SqlRoleProvider caches the current User’s Roles for the lifetime of a page-request?

For example, I make use of Roles in attributes on Controller methods:

[Authorize(Roles = "Admin")]

and custom code

if (user.IsInRole(MembershipRole.Admin))
{
     // Do something
}
else if (user.IsInRole(MembershipRole.Printer))
{
     // Do something else
}

If the Role Provider does not cache roles, is the best solution to write a custom Role Provider that inherits from the default one, and override the methods to get the Roles once and cache them for the Request duration? Can this be done in a way that both the Authorize attribute and my own code will make use of the cached roles?

(In case you were wondering, I don’t want to use the cacheRolesInCookie web.config option to cache the roles in cookies).

Thanks in advance for any suggestions.

[Edit to include details triggered from Joe’s answer]

I decompiled System.Web.Mvc.AuthorizeAttribute and the AuthorizeCore method calls the following method for each role to be checked:

httpContext.User.IsInRole

Then peering into System.Web.Security.RolePrincipal (which is what “User” is above) both the methods below do indeed use a cached copy of the User’s roles (or populates the cache if empty):

public string[] GetRoles()
public bool IsInRole(string role)

The cache is stored as a field on User, so its lifetime is for the duration of the request.

The methods find the roles using:

Roles.Providers[this._ProviderName].GetRolesForUser(this.Identity.Name)

so will use whatever role provider you have chosen for the application (default or custom).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:31:41+00:00Added an answer on June 15, 2026 at 8:31 pm

    If you use a RoleProvider in an ASP.NET or ASP.NET MVC application, then HttpContext.User will reference a RolePrincipal which does cache roles for the lifetime of the request.

    However, in a WCF service that uses ASP.NET roles:

    <behavior ...>
        <serviceAuthorization principalPermissionMode ="UseAspNetRoles"
                         roleProviderName ="MyRoleProvider" />
    </behavior>
    

    this is not true: instead HttpContext.User will reference the internal class System.ServiceModel.Security.RoleProviderPrincipal, which does not cache roles: instead it always calls RoleProvider.IsUserInRole.

    The out-of-the-box RoleProviders don’t do any caching, so this can result in repeated connections to the underlying data store. It seems like a deficiency to me: it would have been easy to cache the roles on first access.

    is the best solution to write a custom Role Provider that inherits from the default one, and override the methods to get the Roles once and cache them for the Request duration?

    Not necessary for ASP.NET or ASP.NET MVC, but could be envisaged for WCF. Caching for the Request duration will presumably use HttpContext.Items, so will introduce a dependency on the existence of HttpContext, but that’s not necessarily a problem except for making unit testing harder.

    Can this be done in a way that both the Authorize attribute and my own code will make use of the cached roles?

    If you configure your custom RoleProvider in web.config there’s nothing more you need to do so that the Authorize attribute will use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use ASP.NET MVC 3 for an application which makes heavy use or Users
I have a .NET MVC application which makes extensive use of AJAX and modal
My application makes several calls to an Action method (ASP .NET MVC) which returns
The MVC application that I am building has a portion of the page (implemented
I have an ASP.NET MVC 2 application which in part allows a user to
I am building an asp.net mvc web application. Do I need to use captcha
I have an MVC 2 application that every page requires authorization on (except currently
I have a question concerning the structure of an MVC application. Suppose we have
I have an ASP.NET MVC web application that makes REST style web service calls
I have written an ASP.NET MVC application that allows the user to specify their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.