Using a technique that I found in a recent ASP.NET MVC book, I have a controller action method that returns a partial view for an ajax request and a full action view for a regular receive request - it checks the IsAjaxRequest property of the Request object to determine what type of action needs to be returned. The partial view returned by the action method returns HTML for displaying a table of records from the database. The controller containing the action method is marked with the Authorize attribute, so only registered users can call controller methods. I use forms authentication with a 30 minute timeout and expiration.
The problem occurs after the user has reached the 30 minute timeout. Since the controller is marked with the Authorize attribute, calling the action method after the timeout exposes the user to the login page. However, since this is an ajax call, the html for my login page is returned and displayed in the middle of the page, which should contain an HTML record table, which is usually returned by the action method in a partial view. The ajax call doesn't actually work, just returning html for the wrong page.
Has anyone encountered and encountered this problem? I try not to move all my code on the server side, which processes ajax calls to a separate controller, which does not require an authenticated user, but this seems like my only alternative at the moment. Even this will not lead to the behavior that I would expect, because it will allow the user to continue using the web page even after the 30-minute timeout has been reached - it will not be redirected to the login page.
Thanks for any advice.
Edit
The solution below with a custom attribute, AuthorizeAttribute, seems to direct me in the right direction, but I can't even get to this code. It appears that the code in the custom attribute, AuthorizeAttribute, is never reached after the expiration timeout. Forms authentication seems to cause a redirect to the login page long before the attribute code. The custom attribute AuthorizeAttribute is the only one on my controller. I also have the following web.config values ββ(the timeout value is set extremely low to cause a timeout for testing):
<authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="1" slidingExpiration="true" defaultUrl="~/ErrorReport/Index" requireSSL="true" protection="All"/> </authentication> <authorization> <deny users="?"/> <allow users="*"/> </authorization> <location path="Content"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location> <location path="Scripts"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location>
Are web.config authorization elements interfering? Should I use them with ASP.NET MVC?
source share