Web 表单登录超时

Web Forms Login Timeout

我已经使用个人用户帐户创建了一个开箱即用的 Web Forms 4.5.2 项目。我无法让登录超时生效。我更新了 Web.config 以将超时设置为 12 分钟:

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login.aspx" timeout="12" slidingExpiration="true" 
        requireSSL="true" />
</authentication>

我明确将会话超时设置为 20 分钟,尽管我认为这是默认设置:

<sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="20">

这是我的 understanding that with slidingExpiration set to true that if the time elapsed is greater than half 会话时间,超时会在浏览器刷新时重置。除了 slidingExpiration,超时只是不起作用,因为我在 12 分钟后刷新浏览器时仍然登录。

当这不起作用时,我查看了 Startup.Auth.cs 文件并将那里的时间间隔也更改为 12 分钟。我推测这与 cookie 的过期有关:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = 
                SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,
                ApplicationUser>(validateInterval: TimeSpan.FromMinutes(12),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });

我正在使用 SQL Server 2014 的本地安装实例:

<add name="DefaultConnection" 
    connectionString="Server=XXX;Database=abc;user ID=myself;password=myself123" 
        providerName="System.Data.SqlClient" />

在IIS中,会话状态设置为"In process",cookie设置如下:

还是不行。我错过了什么?

更新

我在我的本地开发机器上添加了一个自签名证书,但仍然无法超时工作;不断登录。我是否必须编写特定代码才能获得此功能?到目前为止我只用过旧的会员系统,对Owin/Katana/Identity/EF.

不是很熟悉

我终于弄明白了,虽然我真的需要在 OWIN/Katana 上加把劲。

首先,我从Web.config中注释掉了以下内容,因为它仅用于较旧的会员系统:

<!--<forms loginUrl="~/Account/Login.aspx" timeout="1" slidingExpiration="true" requireSSL="true" />-->

接下来,我按照this article在Startup.cs代码中配置了认证超时:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
           OnValidateIdentity = 
           SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
             validateInterval: TimeSpan.FromMinutes(10),
             regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        },
        SlidingExpiration = true,
        ExpireTimeSpan = TimeSpan.FromMinutes(20)
    });

重要提示:为了完成这项工作,我不得不手动删除浏览器开发工具中的旧 cookie。

更新

特此说明,Web.config 文件中的表单条目仍由身份成员系统使用。您可以在那里设置全局超时,但如果您想要更精细的控制,请使用上面的代码片段。