简体   繁体   English

会话超时后自动重定向到登录

[英]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.

相关问题 会话超时后重定向到登录页面 - Redirect to login page after session is timeout 会话超时后重定向回登录页面ASP.NET 5 MVC 6 - Redirect back to login page after session timeout ASP.NET 5 MVC 6 session 超时后如何重定向到主页 - How to redirect to home page after session timeout 实施自动登录链接(并在此之后立即结束会话) - Implementing an Auto LogIn link (and ending the session right after that) 会话超时时自动重定向ASP.NET C#(WebForms) - Auto Redirect when Session Timeout ASP.NET C#(WebForms) 使用 Azure AD 和 OAuth2.0 在 Session 空闲超时上实现自动重定向 - Implement Auto Redirect on Session Idle timeout using Azure AD and OAuth2.0 有没有简单的方法在会话超时后重定向到会话过期页面而不重定向到formauthentication loginUrl - Is there any easy way to redirect to session expire page after session timeout without redirecting to formauthentication loginUrl 当会话在具有母版页的网站上过期时,如何自动重定向到登录页面 - How to auto redirect to login page when session expires on a site with master page 如何重定向和自动登录用户 - How to redirect and auto login user 在asp.net mvc中注册成功后用户名自动填写登录页面(重定向到登录) - Username auto fill in Login Page after Register success (Redirect to Login) in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM