Authentication CookieAutenticationHandler中忽略Cookie过期值

Authentication CookieAutenticationHandler中忽略Cookie过期值,authentication,asp.net-core,cookies,Authentication,Asp.net Core,Cookies,我一直在尝试设置会话cookie的过期时间。因此,首先,我以以下方式配置了Cookie处理程序: services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies", options => { options.Cookie.Expiration =

我一直在尝试设置会话cookie的过期时间。因此,首先,我以以下方式配置了Cookie处理程序:

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies", options => 
{
    options.Cookie.Expiration = TimeSpan.FromSeconds(3600);
})
.AddOpenIdConnect(...
虽然看起来很简单,但事实并非如此。cookie始终创建为会话(而不是持久化)

我一直在探索
CookieAuthenticationHandler
,我发现:

protected async override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
{
    if (user == null)
    {
        throw new ArgumentNullException(nameof(user));
    }

    properties = properties ?? new AuthenticationProperties();

    _signInCalled = true;

    // Process the request cookie to initialize members like _sessionKey.
    await EnsureCookieTicket();
    var cookieOptions = BuildCookieOptions();
    ...
}
令人惊讶的是,在方法
BuildCookieOptions
中将我在启动中配置的过期设置为null:

private CookieOptions BuildCookieOptions()
{
    var cookieOptions = Options.Cookie.Build(Context);
    // ignore the 'Expires' value as this will be computed elsewhere
    cookieOptions.Expires = null;

    return cookieOptions;
}
它说“Expires”值将在别处计算

最后,我在另一篇文章中看到,当使用类似OpenId使用的RemoteAuthenticationHandler时,该处理程序会触发一个OnticketReceived事件,通过在OpenId处理程序的配置中实现此事件可以设置过期时间:

.AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = "Cookies";
    options.ClientId = "client";

    options.Events.OnTicketReceived = async (context) => 
    {
        context.Properties.ExpiresUtc = DateTime.UtcNow.AddSeconds(3600);
        context.Properties.IsPersistent = true;
    };
但是,为什么绝对忽略这个过期值呢?为什么CookieAuthenticationOptions允许我设置此属性,但处理程序要做的第一件事是将其设置为null?这房子有用吗


或者只是我遗漏了什么?

使用
options.ExpireTimeSpan
而不是
options.Cookie.expirement
(是的,它被忽略)。您仍然需要将
IsPersistent
设置为
true
,以便cookie使用过期时间,而不是默认的基于会话的生存期。谢谢,也许我会使用
选项。ExpireTimeSpan
,但此属性与票证过期有关,因此我仍然没有持久cookie(如果用户关闭浏览器,我将丢失会话)。如果我想要有一个peristent Cookie,我找到的唯一方法就是添加
选项.Events.onticketreceived
lambda表达式配置
ExpiresUtc
isperistent=true
属性,在
OpenIdConnectOptions
上。我仍然不明白为什么在我的Cookie中配置
expirement
德勒的签名行为不受尊重。