【问题标题】:How to set the cookie validateInterval in ASP.NET Core?如何在 ASP.NET Core 中设置 cookie validateInterval?
【发布时间】:2016-09-16 02:43:37
【问题描述】:

我正在尝试为使用 ASP.NET Identity 3 的 ASP.NET 5 RC1 应用程序设置 validateInterval

我正在尝试实现this答案中的代码。

有很多像this answer 这样的代码示例,但它似乎在 ASP.NET 5 RC1 中无效

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(15))
    },
    ExpireTimeSpan = TimeSpan.FromMinutes(30)
});

如果我尝试在ASP.NET 5 RC1 中使用上面的代码示例,我就不行了

Provider 不是CookieAuthenticationOptions 的属性 并且 Visual Studio 无法通过其灯泡选项在任何命名空间中找到 CookieAuthenticationProvider

如何在ASP.NET 5 RC1 中设置validateInterval

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc asp.net-identity asp.net-identity-3


    【解决方案1】:

    验证间隔在 IdentityOptions 中设置:

    services.AddIdentity<AppUser, AppRole>(options =>
    {
        options.SecurityStampValidationInterval = TimeSpan.FromMinutes(15);
    }
    

    您可以使用 CookieAuthenticationEvents 附加到验证事件:

    app.UseCookieAuthentication(new CookieAuthenticationOptions()
    {
        Events = new CookieAuthenticationEvents()
        {
            OnValidatePrincipal = context =>
            {
                Microsoft.AspNet.Identity.SecurityStampValidator.ValidatePrincipalAsync(context);
                return Task.FromResult(0);
            },
        },
        ExpireTimeSpan = TimeSpan.FromMinutes(30)
    });
    

    【讨论】:

    • 或者干脆return SecurityStampValidator.ValidatePrincipalAsync(context);
    【解决方案2】:

    从 ASP.NET Core 2.0 开始,当您 AddIdentity 时,您将无法设置 SecurityStampValidationInterval

    您将能够通过SecurityStampValidatorOptions 设置ValidationInterval

            services.Configure<SecurityStampValidatorOptions>(options =>
            {
                options.ValidationInterval = TimeSpan.FromSeconds(10);
            });
    

    P.S:你必须先AddIdentity,然后再ConfigureApplicationCookie

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2021-12-14
      • 2021-08-03
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多