【问题标题】:How to timeout a user in asp.net formsAuthentication如何在 asp.net formsAuthentication 中使用户超时
【发布时间】:2009-11-08 02:31:25
【问题描述】:

我想知道如果用户在 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”,因为如果他们选中“保持登录状态 2 周”,我想登录 2 周。我还确保在我的 cookie Cookie 中启用 IsPersistent。

那么我需要设置另一个超时吗?由于在我的网站上闲置一段时间后,它不再起作用。我没有计时,但我说如果我离开并在 10 分钟后回来并尝试在我的网站上做一些事情,比如保存一些它不起作用的东西。所以看起来我的连接被杀死了。我必须退出,重新登录,然后它才能工作

编辑

这就是我制作饼干的方式

 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。

标签: .net asp.net asp.net-mvc web-config


【解决方案1】:

另一个答案,只是为了说明您可能希望如何使用 web.config 中的值创建 cookie,而不是在代码中硬编码它们。

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

FormsAuthentication.RedirectFromLoginPage("Bob", isPersistent)

但是,如果您需要将 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】:

    我假设你的超时是由 Session Timeout 而不是 Authentication Timeout

    检查 web.config 中的 会话状态 节点。

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

    【讨论】:

    • 我的 webconfig 中没有会话状态。它默认是什么?
    • "Session" 在您的问题中被提及。这就是我所指的 属性。
    • 我不知道有一个单独的会话状态。我认为这就是 formAuth 所关心的。
    • 在创建 formsauth 票证时检查“DateTime.Now.AddMinutes(30), // expires”。 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ( 1, // version txtEmail.Text, // name DateTime.Now, // issueDate DateTime.Now.AddMinutes(30), // expiration false, // isPersistent roles, // userData FormsAuthentication.FormsCookiePath // cookie路径 ); weblogs.asp.net/owscott/archive/2006/07/15/…
    • 默认会话超时为 10 分钟。 msdn.microsoft.com/en-us/library/ms525473.aspx
    【解决方案3】:

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

    请参阅我对this SO question 的回答,了解概述和教程链接以了解 ASP.NET 中的表单身份验证。

    更新:

    我如何为用户设置超时,如果 他们说之后不做任何要求 10mins 有会话被杀死和 他们已注销

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

    简单的答案是不要在会话中存储数据。请参阅this SO question,这似乎与您想要的相似。

    【讨论】:

    • 对不起,我不关注(或者我认为我不关注)。我没有设置滑动到期日期。我只有一个绝对到期时间,即会话结束或登录日期后 2 周。
    • 我已经用另一个链接更新了我的答案,该链接指向另一个关于 ASP.NET 和会话/身份验证的答案。请阅读参考信息,尤其是链接和教程视频。
    • 我现在将它设置为滑动,但它仍然无法让我在 2 周内保持登录状态。我不知道为什么。
    • @chobo2,你不能滑动并说“但最多只能使用 2 周”。另外,你要关闭浏览器吗?如果您没有将身份验证 cookie 设置为持久性,则 cookie 本身不会保存到磁盘,因此当您重新打开浏览器时不会登录。另请参阅 msdn:msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多