IsInRole Cachehttp://forums.asp.net/t/1767049.aspx/1?IsInRole+CacheWed, 08 Feb 2012 09:28:30 -050017670494821360http://forums.asp.net/p/1767049/4821360.aspx/1?IsInRole+CacheIsInRole Cache <p>Hi.</p> <p>I am creating my own &quot;Roles&quot;.</p> <p>I have a table of Roles in the DB and a table which holds which users belong to which roles.</p> <p>&nbsp;</p> <p>I then want the ability in ASP.NET to see if a user belongs to a role using IsInRole(<em>RoleName</em>);</p> <p>&nbsp;</p> <p>I understand I need to create a CustomPrincipal and implement the IPrincipal interface and for the IsInRole method, create the procedure I want to validate the parameter against a datasource.</p> <p>Problem here is performance where it means I have to go to the DB everytime to see if the user (stored in session) belongs to that role.</p> <p>&nbsp;</p> <p>Is there a way to cache the data for checking to see if the current user is in a role?</p> <p>Maybe im missing something or a better way of doing this? I must stress that I want to use the User.IsInRole(&quot;...&quot;) method.</p> <p></p> 2012-02-07T22:11:15-05:004821844http://forums.asp.net/p/1767049/4821844.aspx/1?Re+IsInRole+CacheRe: IsInRole Cache <p>string username = (Login1.FindControl(&quot;UserName&quot;) as TextBox).Text;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (Roles.IsUserInRole(username, &quot;YourRoleHere&quot;))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Redirect(&quot;~/member/default.aspx&quot;);</p> 2012-02-08T06:33:33-05:004821912http://forums.asp.net/p/1767049/4821912.aspx/1?Re+IsInRole+CacheRe: IsInRole Cache <p>That does not answer the question AT ALL.</p> 2012-02-08T07:03:59-05:004822163http://forums.asp.net/p/1767049/4822163.aspx/1?Re+IsInRole+CacheRe: IsInRole Cache <p>Ideally you should create a custom Role Provider. You can find plenty of articles how to do that, but you can also download the code for the existing providers from download.microsoft.com/download/a/b/3/ab3c284b-dc9a-473d-b7e3-33bacfcc8e98/ProviderToolkitSamples.msi and just modify it.</p> <p>ASP.NET improves performance by caching the roles in a cookie. This cookie is set when the user logs in and is passed to the application upo each request. The role manager can then check the cookie instead of the database (but check the db if no cookie exists). Alternatively, within the provider, you could cache the roles per user in the ASP.NET Cache object.</p> 2012-02-08T09:28:30-05:00