表单身份验证超时后重定向到页面

时间:2011-06-15 20:59:14

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

在我的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()

使用用户名和bool来保存cookie,但这是用于创建一个不能获取它的auth cookie。那么,var cookie,第一行代码是什么样的。

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

可能已经找到了如何获取cookie,

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

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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