[英]Auto redirect to login after session timeout
我會在會話超時后嘗試自動重定向到我的登錄頁面。 我嘗試在Main.Master頁面中添加此代碼(所有其他頁面都連接到此母版頁):
protected void Page_Load(object sender, EventArgs e)
{
//Redirects to Login Page 3 seconds before session timeout
Response.AppendHeader("Redirect", Convert.ToString((Session.Timeout * 60) - 3) + "; URL=~/Login.aspx");
}
我在我的網絡配置中將會話超時配置為1分鍾:
<sessionState mode="InProc" cookieless="false" timeout="1"/>
但沒有任何反應
任何人都可以幫我找到這個代碼的問題,或者有其他想法如何讓它工作?
編輯 :來自web.config的身份驗證節點
<authentication mode="Forms">
<forms name=".CAuthenticated" loginUrl="Login.aspx" protection="All"
timeout="20"/>
</authentication>
protected void Page_Init(object sender, EventArgs e)
{
if (Session["Username"] == null)
{
Response.Redirect(ResolveClientUrl("~/login.aspx") + "?returnURL=" + HttpContext.Current.Request.Url.AbsolutePath);
}
else
{
lblUsername.Text = Session["Username"].ToString();
}
}
如果在發送HTTP標頭后附加“標頭”,則會將AppendHeader
記錄為導致異常。您需要確保在發送HTTP標頭之前調用AppendHeader。 根據您的母版頁, Load
事件可能為時已晚。 您可以嘗試使用Init
事件。
這是我有一個適合我的例子:
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" defaultUrl="~/Admin" timeout="20">
</forms>
</authentication>
如果您有此功能,則無需自行檢查超時cookie。 這假設您正在使用表單身份驗證。
我認為你需要在標題中使用Refresh
而不是`Redirect':
Response.AppendHeader("Refresh",
Convert.ToString((Session.Timeout * 60) - 3) +
";URL=~/Login.aspx");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.