c# - How to cache items within a filterAttribute -
how use caching maybe attribute along? not having make lot of calls container.getservice , getting user. put caching place cache identity key , planner value , up?
public class adminsandprogrammanagersonlyattribute : filterattribute, iauthorizationfilter { public adminsandprogrammanagersonlyattribute() { order = 1; //must come after authenticateattribute } public void onauthorization(authorizationcontext filtercontext) { var userrepository = globalapplication.container.getservice<irepository<projectplannerinfo>>(); var identity = filtercontext.httpcontext.user.identity; var planner = userrepository.getall().whereemailis(identity.name); if (!planner.isinrole("db admin") || planner.programmanager == 1) { filtercontext.result = new redirecttorouteresult("error", new routevaluedictionary(new { controller = "error", action = "insufficientprivileges", reason = "contract" })); } } }
soo...
something this?
public void onauthorization(authorizationcontext filtercontext) { var identity = filtercontext.httpcontext.user.identity; if (filtercontext.httpcontext.cache[identity.name] == null) { if (filtercontext.httpcontext.cache["repo"] == null) { filtercontext.httpcontext.cache.add("repo", globalapplication.container.getservice<irepository<projectplannerinfo>>(), null, datetime.now.addseconds(30), cache.noslidingexpiration, cacheitempriority.normal, null); } var userrepository = filtercontext.httpcontext.cache["repo"] irepository<projectplannerinfo>; filtercontext.httpcontext.cache.add(identity.name, userrepository.getall().whereemailis(identity.name), null, datetime.now.addseconds(30), cache.noslidingexpiration, cacheitempriority.normal, null); } var planner = filtercontext.httpcontext.cache[identity.name] projectplannerinfo; if (!planner.isinrole("db admin") || planner.programmanager == 1) { filtercontext.result = new redirecttorouteresult("error", new routevaluedictionary(new { controller = "error", action = "insufficientprivileges", reason = "contract" })); } }
you use built-in cache:
filtercontext.httpcontext.cache
Comments
Post a Comment