Page Redirect when session end in ASP.Net MVC
I would like to do redirect to login when current session end and that config must be working at any View and Controller.
My current code in Global.asax:
protected void S开发者_高级运维ession_End(object sender, EventArgs e)
{
Session.Abandon();
//GetPath() is getting currently path
// eg. http://localhost/mymvcproject
Response.Redirect(PATH.GetPath() + "User/LogOn");
}
Check the following setting under <system.web> in your web.config file:
<sessionState mode="InProc" cookieless="false" timeout="1"></sessionState>
then fill the following text in your site.Master
if (Session.IsNewSession)
{
Response.Redirect(PATH.GetPath() + "User/LogOn");
}
I don't think your code can work because Session_End()
is more usually invoked when there is NO request made by the browser after a specific duration. Therefore, Response
here would correspond to no particular request, and thus, no redirection.
Instead, try to handle Application_Start
and check for Session.IsNew
property. If it's true
, then perform the redirection. (Consider doing that by invoking FormsAuthentication.RedirectToLoginPage()
though.)
When checking for IsNew
, beware of the situation described here. I guess assigning some dummy session variable during the login process will address that, although I haven't tried myself.
精彩评论