I'm confused on ASP.NET MVC caching and authorization and in dire need of some clarification.
My self-made authorization attribute inherits from AuthorizeAttribute
. Its overridden AuthorizeCore
method runs every time, even if I set an [OutputCache]
attribute on a controller action. I understand that part.
Now the mind bender for me: AuthorizeCore
will fail every time now when I actually do output caching and the page is served from the cache. The reason is that when the request is cached, the httpContext.Session
supplied with AuthorizeCore
is null
!? Here's some simplified code:
protected override bool AuthorizeCore(HttpContextBase httpContext) {
return (Session["userId"] != null)
}
So if httpContext.Session
is null
, this obviously fails every time. I need to access the session though, how else can I check if the request is authorized? This doesn't make any sense - if this is how it should be then I would never be able to use cached pages together with authentication in ASP.NET MVC. Help?