title: Does Role Provider cache per request?
body: <p>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?</p>
  
  <p>For example, I make use of Roles in attributes on Controller methods:</p>
  
  <pre><code>[Authorize(Roles = "Admin")]
  </code></pre>
  
  <p>and custom code </p>
  
  <pre><code>if (user.IsInRole(MembershipRole.Admin))
  {
       // Do something
  }
  else if (user.IsInRole(MembershipRole.Printer))
  {
       // Do something else
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>(In case you were wondering, I don't want to use the cacheRolesInCookie web.config option to cache the roles in cookies).</p>
  
  <p>Thanks in advance for any suggestions.</p>
  
  <p><strong>[Edit to include details triggered from Joe's answer]</strong></p>
  
  <p>I decompiled System.Web.Mvc.AuthorizeAttribute and the AuthorizeCore method calls the following method for each role to be checked:</p>
  
  <pre><code>httpContext.User.IsInRole
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>public string[] GetRoles()
  public bool IsInRole(string role)
  </code></pre>
  
  <p>The cache is stored as a field on User, so its lifetime is for the duration of the request.</p>
  
  <p>The methods find the roles using:</p>
  
  <pre><code>Roles.Providers[this._ProviderName].GetRolesForUser(this.Identity.Name)
  </code></pre>
  
  <p>so will use whatever role provider you have chosen for the application (default or custom).</p>