簡體   English   中英

如何在 ASP.NET Core 中設置 cookie validateInterval?

[英]How to set the cookie validateInterval in ASP.NET Core?

我正在嘗試為使用ASP.NET Identity 3的 ASP.NET 5 RC1 應用程序設置validateInterval

我正在嘗試實現答案中的代碼。

有很多類似這個答案的代碼示例,但它似乎在 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

驗證間隔在 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)
});

從 ASP.NET Core 2.0 開始,您將無法在AddIdentity時設置SecurityStampValidationInterval

您將能夠通過SecurityStampValidatorOptions設置ValidationInterval

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

PS:您必須先AddIdentityConfigureApplicationCookie之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM