c# - Per-Request static data in ASP.NET -


i'm wanting cache roles user in each request comes in. there several places throughout given page where have like:

<% if(roles.isuserinrole("rolename")) {%>     <!-- conditional rendering --> <% } else if(roles.isuserinrole("anotherrole") {%>     <!-- point --> <% } %> 

since stored in sql database, each of these requests hit database. know there ways cache roles in cookie, don't want that. anyway, thinking this.

public static class securityutils     {         public static string[] userroles()         {             var context = httpcontext.current;              if (context == null) return enumerable.empty<string>();              string[] roles;              roles = context.items["userroles"] string[];              if (roles == null)             {                 roles = roles.getrolesforuser();                 context.items["userroles"] = roles;             }              return roles;         }     } 

anyone see issues this? know ever call userroles() lookup item in context , maybe isn't effecient thing do. want know if cache on per-request basis there's no overlap other users request.

that looks safe enough @ quick glance. httpcontext.current.items per-http request cache. option consider further reduce database calls use session state.

consider scenario have page lot of ajax stuff going on. each ajax request invoke database call load security roles, since separate http requests.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -