.Net MVC Partial View load login page when session expires .Net MVC Partial View load login page when session expires ajax ajax

.Net MVC Partial View load login page when session expires


Maybe a hacky answer, but you can change the redirect location in forms authentication to a page that sets the window location to the login page with javascript.

Web Config

<authentication mode="Forms">  <forms loginUrl="~/Account/RedirectToLogin" timeout="2880" /></authentication>

Account Controller

public ActionResult RedirectToLogin(){    return PartialView("_RedirectToLogin");}

_RedirectToLogin View

<script>    window.location = '@Url.Action("Login", "Account")';</script>


The issue is your call is intercepted by [Authorize] and sends the login page even before your action method code is called. One way to sort this out is to create a custom action filter to check the timeout and do a hard redirect to login page. Following post has a good write up which may help you in creating and registering the filter

http://www.codeblockdrive.com/2012/12/mvc-custom-filters-session-timeout.html

Best of luck


You may want to check the answer to this (similar) question.

ASP.NET MVC Partial view ajax post?

Basically it says that you should avoid making ajax calls to functions that may redirect because of this and other problems.

You can avoid the problem that you are having by authorizing / checking the expiration manually in your function, and then returning redirect information that can be applied to the whole page.

I have used this approach, and it works well.