Caching question - anyone cache/cookie roles in .net web app? HttpContext etc?
Posted on 2013-05-25
I have an MVC application and in the app I have a custom AuthorizeAttribute. In this method that gets called a good amount I run a query to check if the logged on user User.Identity.Name is in a SQL table. That is the first check then if that is valid I think check if a role exists in a SQL role table and pass back simply a true boolean else false.
Is there a way to cache this like in the HttpContext somewhere? So even though the method will always get called to check the role I can first check if the roles are in cache or a cookie etc. instead of making the database call again. The same for the first check if the user is a valid user. I tried like setting the HttpContext user IsAuthenticated property but that is not allowed. I think there is a way to set the IPrincipal or something. The idea is I don't want to use the role and membership provider I want to cache these somehow or put them in the right cookie similar but not have to stub out all the interface methods with Not implemented etc.