【问题标题】:Redirect to page after Forms Authentication Timeout表单身份验证超时后重定向到页面
【发布时间】:2011-09-15 21:30:51
【问题描述】:

在我的 asp.net web 应用程序中,我使用 asp.net 表单身份验证,配置如下。

<authentication mode="Forms">
    <forms name=".ASPNETAUTH" loginUrl="Login.aspx" protection="None" timeout="20" />
</authentication>

表单身份验证超时后,我想重定向到不同的页面。例如到 'SessionTimedOut.aspx' 页面。

我在这里找到了其他问题,这是一个,Forms Authentication Timeout vs Session Timeout

给出的答案是有道理的,但第一行代码让我感到困惑。

var cookie = Retrieve AuthenticationCookie();

if (cookie == null) return;

FormsAuthenticationTicket ticket = null;

try {
    ticket = FormsAuthentication.Decrypt(cookie.Value);
} catch (Exceptoin decryptError) {
    // Handle properly
}

if (ticket == null) return; // Not authorised

if (ticket.Expiration > DateTime.Now) {
    Response.Redirect("SessionExpiredPage.aspx"); // Or do other stuff here
}

现在有一个

FormsAuthentication.GetAuthCookie()

它需要一个用户名和布尔值来持久化cookie,但这是为了创建一个没有得到它的auth cookie。那么,var cookie 的第一行代码是什么样的。

目前,我在网络配置中使用 " ,然后当用户登录设置会话时,然后在我的基本页面的页面 init 中的每个帖子返回时,我正在检查该会话是否为空,如果是,则重定向到会话超时页面。这不是我真正想要的。

可能已经知道如何获取 cookie,

HttpCookie cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);

这不起作用,因为当身份验证票证到期时,cookie 消失并且 cookie var 为空。还有其他方法可以让这个工作吗?我仍然希望在回发时检查身份验证是否已过期,然后采取适当的措施。任何人的任何想法????

【问题讨论】:

    标签: c# asp.net session forms-authentication session-timeout


    【解决方案1】:

    问:那么,第一行代码的 var cookie 会是什么样子?

    var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]

    【讨论】:

    • 谢谢,这正是我所需要的。刚刚发现自己回答了一会儿。
    【解决方案2】:

    要记住的是,即使您的会话在服务器端超时,客户端在下一个请求之前不会处理任何内容。那时它会发现它的会话已经过期并尝试重新启动会话。 Response.Redirect 甚至 Server.Redirect 调用都无济于事。

    您需要做的是将您的服务器超时与您的客户端超时同步,并有一些客户端脚本将用户重定向到“超时”类型的页面。我写了一篇文章,其中包含一些关于如何做到这一点的示例代码here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多