[英]Log out and redirect user to login screen ASP NET MVC / C #
我正在尋找一種方法來結束會話,並在系統給出TimeOut時將用戶重定向到登錄屏幕。
根據我研究過的一些示例,我嘗試使用Session.Abandon()。 但是我不知道我在做什么錯。 以下是我在Global.asax中要做的代碼:
protected void Application_EndRequest(object sender, EventArgs e)
{
var context = new HttpContextWrapper(Context);
if (context.Response.StatusCode == 302 && context.Request.IsAjaxRequest())
{
var redirectLocation = context.Response.RedirectLocation.ToString();
context.Response.RedirectLocation = null;
context.Response.ContentType = "text/plain";
context.Response.Write("session_timeout;" + redirectLocation);
Session.Abandon();
Response.Redirect("~/Account/Login");
}
}
該代碼僅運行到:context.Session.Abandon();。 除非刷新頁面,否則不會重定向到登錄屏幕。
public ActionResult LogOff()
{
HttpContext.Session.Remove(SessionKeys.UserType);//This will remove all keys from session variable. For example, if your session contains id, name, phone number, email etc.
HttpContext.Session.RemoveAll();//This will remove all session from application
FormsAuthentication.SignOut();
return RedirectToAction("Login", "Account");
}
我可以這樣解決我的問題:
1-我通過添加以下標記在Web.config中設置超時時間:
<sessionState timeout="16"></sessionState>
2-我用javascript在veridifca中創建了一個函數,如果超時時間結束了:
var sessionTimeoutWarning = @Session.Timeout- 1;
var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
setTimeout('SessionEnd()', sTimeout);
function SessionEnd() {
alert("Your session has expired");
window.location = "/Account/SessionTimeOutLogOff";
}
注意:此函數放在我的_Layout.chshtml中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.