首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Forms身份验证超时与会话超时

Forms身份验证超时与会话超时
EN

Stack Overflow用户
提问于 2009-09-24 10:03:05
回答 2查看 100.3K关注 0票数 63

在我的asp.net网站中,我使用具有以下配置的asp.net表单身份验证

代码语言:javascript
复制
<authentication mode="Forms">
    <forms loginUrl="~/Pages/Common/Login.aspx"
           defaultUrl="~/Pages/index.aspx"
           protection="All"
           timeout="30"
           name="MyAuthCookie"
           path="/"
           requireSSL="false"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" >
    </forms>
</authentication>

我有以下问题

  1. 会话的超时值应该是什么,因为我在表单身份验证中使用了可变过期时间,这取决于哪个会话将在表单身份验证之前过期。我如何保护它?
  2. 在正式身份验证注销后,我想将页面重定向到logout.aspx,但它自动将我重定向到loginpage.aspx。这怎么可能?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-24 10:08:50

  1. 为安全起见: TimeOut(Session) <= TimeOut(FormsAuthentication) *2
  2. 如果您想在身份验证超时后显示loginUrl属性中指定的页面以外的其他页面,则需要手动处理,因为ASP.NET不提供这样做的方法。

要实现#2,您可以手动检查cookie及其AuthenticationTicket是否过期,如果过期,则重定向到您的自定义页面。

您可以在以下事件之一中执行此操作:AcquireRequestStateAuthenticateRequest

事件中的示例代码可能如下所示:

代码语言:javascript
复制
// Retrieve AuthenticationCookie
var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) return;
FormsAuthenticationTicket ticket = null;
try {
    ticket = FormsAuthentication.Decrypt(cookie.Value);
} catch (Exception decryptError) {
    // Handle properly
}
if (ticket == null) return; // Not authorised
if (ticket.Expiration > DateTime.Now) {
    Response.Redirect("SessionExpiredPage.aspx"); // Or do other stuff here
}
票数 54
EN

Stack Overflow用户

发布于 2012-05-31 22:22:50

对于具有会话依赖关系的站点,您可以使用global.asax中的会话启动事件简单地注销过时的身份验证:

代码语言:javascript
复制
void Session_Start(object sender, EventArgs e)
{
  if (HttpContext.Current.Request.IsAuthenticated)
  {

    //old authentication, kill it
    FormsAuthentication.SignOut();
    //or use Response.Redirect to go to a different page
    FormsAuthentication.RedirectToLoginPage("Session=Expired");
    HttpContext.Current.Response.End();
  }

}

这使得新的会话=新的认证,句号。

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1470777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档