Web窗体登录超时

问题描述 投票:0回答:1

我创建了一个带有个人用户帐户的开箱即用的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">

这是我的understandingslidingExpiration设置为true,如果经过的时间是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中,会话状态设置为“正在处理”,cookie设置如下:

enter image description here

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

UPDATE

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

asp.net authentication webforms web-config asp.net-identity
1个回答
0
投票

我终于明白了这一点,虽然我真的需要加强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。

UPDATE

只是为了澄清,身份会员系统仍然使用Web.config文件中的表单条目。您可以在那里设置全局超时,但如果您想要更精细的控制,请使用上面的代码片段。

© www.soinside.com 2019 - 2024. All rights reserved.