如何在asp.net formsAuthentication中设置用户超时时间

3

我想知道如何设置一个超时时间,如果用户在10分钟内没有进行任何请求,则会话将被终止并注销。

在我的webconfig中有以下内容:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn"
                   protection="All"
                   timeout="20160"
                   path="/"
                   requireSSL="false"
                   slidingExpiration="false"
                   defaultUrl="default.aspx"
                   cookieless="UseDeviceProfile"
                   enableCrossAppRedirects="false" />
</authentication>

我被告知将超时设置为“20160”,因为我想在他们勾选“保持登录两周”时保持登录状态两周。我还确保在我的Cookie中启用了IsPersistent。
那么,我需要设置另一个超时时间吗?因为在我的网站上一段时间不活动后,它就不再工作了。我没有计时,但是假设我离开并在10分钟后回来,尝试在我的网站上进行某些操作(例如保存某些内容),它将无法正常工作。所以看起来我的连接已经断开或者出现其他问题。我必须退出,重新登录,然后才能正常工作。
编辑
这是我制作Cookie的方式。
 FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(version,userName,DateTime.UtcNow,DateTime.UtcNow.AddDays(14),createPersistentCookie,userData,"/");
            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            authCookie.Path = "/";
            if (createPersistentCookie == true)
            {
                authCookie.Expires = DateTime.UtcNow.AddDays(14);
            }
            HttpContext.Current.Response.Cookies.Add(authCookie);

当我在Webconfig中设置会话状态时,我的URL中会出现这个。
(S(gkvkze55zfzzee45wj34byee))

我不想在我的代码中出现这个讨厌的行。


关于URL中的sessionID,您需要在web.config中将cookieless =“true”更改为“false”。这样做可以避免在URL中出现SessionID。 - Rasik Jain
3个回答

3

另一个答案,仅仅是为了展示你可能想要使用web.config中的值来创建cookie,而不是在代码中硬编码。

首先,考虑是否需要所有额外选项。最简单的方法是在web.config中设置所有内容。

FormsAuthentication.RedirectFromLoginPage("Bob", isPersistent)

然而,如果您需要向票据添加UserData,则必须创建自己的UserData。请注意,我们使用web.config中的值而不是硬编码的值。

/// <summary>
/// Create a New Forms Authentication Ticket when User Impersonation is active, using the current ticket as a basis for the new ticket.
/// </summary>
private static void NewTicket(MyUser currentUser, 
                              string userData, 
                              bool createPersistentCookie)
{
    System.Web.Configuration.AuthenticationSection authSection =
        (System.Web.Configuration.AuthenticationSection)
        ConfigurationManager.GetSection("system.web/authentication");

    System.Web.Configuration.FormsAuthenticationConfiguration 
        formsAuthenticationSection = authSection.Forms;

    DateTime now = DateTime.Now;

    // see http://msdn.microsoft.com/en-us/library/kybcs83h.aspx
    // Create a new ticket used for authentication
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        2,                                          // Ticket version
        currentUser.UserName,                       // Username to be associated with this ticket
        now,                                        // Date/time issued
        now.Add(formsAuthenticationSection.Timeout),// Date/time to expire
        createPersistentCookie,
        userData,
        FormsAuthentication.FormsCookiePath);

    // Hash the cookie for transport over the wire
    string hash = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(
        FormsAuthentication.FormsCookieName,    // Name of auth cookie (specified in web.config)
        hash);                                  // Hashed ticket

    // Add the cookie to the list for outbound response
    HttpContext.Current.Response.Cookies.Add(cookie);
}

如果用户已经登录,您可以使用相同的技术来重新创建票证。例如,如果您需要更改Ticket.UserData,在发出新的票证时,您将增加版本号。


2
我假设你的超时问题是由于会话超时而不是认证超时导致的。

请检查你的web.config文件中的会话状态节点。

<sessionState mode="InProc"
                    cookieless="true"
                    timeout="60"/>

“Session”在你的问题中提到了。那就是我所指的<sessionstate/>属性。 - Rasik Jain
在创建FormsAuth票证时,请检查“DateTime.Now.AddMinutes(30),//过期时间”。FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ( 1, // 版本 txtEmail.Text, // 名称 DateTime.Now, // 发行日期 DateTime.Now.AddMinutes(30), // 过期时间 false, // 是否持久化 roles, // 用户数据 FormsAuthentication.FormsCookiePath // cookie路径 );http://weblogs.asp.net/owscott/archive/2006/07/15/forms-authentication-timeout.aspx - Rasik Jain
默认的会话超时时间为10分钟。http://msdn.microsoft.com/zh-cn/library/ms525473.aspx - David
为什么超时后它没有将用户重定向到登录页面? - chobo2
Chobo2,我只想到<SessionState/>,<authentication/>用于超时。不确定为什么在你的情况下它不起作用。另外,请尝试检查一下您的表单认证cookie是如何创建的,我的意思是您是否正确设置了过期时间。请检查我之前评论中提供的链接,也许那会有所帮助。还有其他专家可以建议其他方法吗...谢谢。 - Rasik Jain
显示剩余3条评论

0

您不能同时拥有表单身份验证票证的滑动和绝对过期。

请参阅我的这个SO问题的答案,了解ASP.NET中的表单身份验证的概述和教程链接。

更新:

如果用户在10分钟内没有进行任何请求,如何设置超时时间,以便他们的会话被终止并注销

注销=表单身份验证,与会话(状态)(例如存储数据的地方)正交。

简单的答案是不要将数据存储在会话中。请参见这个SO问题,它似乎类似于您想要的内容。


抱歉,我不明白(或者我认为我不明白)。我没有设置滑动过期日期。我只有一个绝对的过期时间,可能是会话结束或从登录日期算起的两周后。 - chobo2
我已经更新了我的答案,并提供了另一个关于ASP.NET和会话/身份验证的答案链接。请阅读所引用的信息,特别是链接和教程视频。 - Robert Paulson
我已经设置了滑动验证,但仍不能让我保持登录状态两周。我不太确定原因是什么。 - chobo2
@chobo2,你不能同时要求滑动过期时间并说“但最多只有2周”。另外,你是否关闭了浏览器?如果你没有将身份验证cookie设置为持久性,那么cookie本身将不会保存到磁盘上,因此当你重新打开浏览器时将无法登录。请参阅msdn:http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.slidingexpiration.aspx - Robert Paulson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接