I have an MVC 4.0 website with Forms Authentication and am attempting to handle Authentication timeout. Specifically, I need to handle ajax
requests differently after a timeout than regular requests because if left to its own devices, MVC's Forms Authentication system will send a 302 Found
(Redirect
) to the Login page as a response to the ajax
request... this ends with the ajax call receiving a 200 Success
HttpStatusCode, when that is obviously not what I want to send!
How can I handle the unauthenticated ajax
requests?
Extending the AuthorizeAttribute
is, of course, not the answer because Authorization only comes in after Authentication. HandleUnauthorizedRequest
is never called in this situation (as opposed to the different answers in the question)
I can intercept every request in the global.asax
and check it there... but that seems like the wrong way to go about it.
Authorization in my web.config is set like so:
<authentication mode="Forms">
<forms loginUrl="Login"
protection="All"
timeout="60"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="Main"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
<sessionState mode="InProc" timeout="60"></sessionState>