[英]Auto redirect to login after session timeout
I am trying to redirect automatically to my login page after session times out. 我会在会话超时后尝试自动重定向到我的登录页面。 I tried to add this code in my Main.Master page (all the other pages are connected to this master page): 我尝试在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");
}
I configured the session timeout to 1 minute in my web config: 我在我的网络配置中将会话超时配置为1分钟:
<sessionState mode="InProc" cookieless="false" timeout="1"/>
but nothing happens 但没有任何反应
Can anyone help me find the problem with this code, or has other ideas how to make it work? 任何人都可以帮我找到这个代码的问题,或者有其他想法如何让它工作?
Edit : Authentication node from web.config 编辑 :来自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();
}
}
AppendHeader
is documented as causing an exception if "header is appended after the HTTP headers have been sent" You need to make sure AppendHeader is called before the HTTP headers have been sent. 如果在发送HTTP标头后附加“标头”,则会将AppendHeader
记录为导致异常。您需要确保在发送HTTP标头之前调用AppendHeader。 Depending on your master page, the Load
event might be too late. 根据您的母版页, Load
事件可能为时已晚。 You could try the Init
event instead. 您可以尝试使用Init
事件。
Here is an example I have that works for me: 这是我有一个适合我的例子:
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" defaultUrl="~/Admin" timeout="20">
</forms>
</authentication>
If you have this, there is no need for you to check the timeout cookie yourself. 如果您有此功能,则无需自行检查超时cookie。 This is assuming you are using Forms Authentication. 这假设您正在使用表单身份验证。
I think you need to use Refresh
instead of `Redirect' in your header: 我认为你需要在标题中使用Refresh
而不是`Redirect':
Response.AppendHeader("Refresh",
Convert.ToString((Session.Timeout * 60) - 3) +
";URL=~/Login.aspx");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.