使用PasswordSignInAsync登录时如何更改Cookie的过期时间?

时间:2020-03-19 13:24:09

标签: c# asp.net-core identityserver4 asp.net-core-identity asp.net-core-3.1

使用以下代码登录时如何更新Cookie过期时间

var result = await _signInManager.PasswordSignInAsync(userName, password, isPersistent, lockoutOnFailure);

就像可以与SingInAsyn一起使用

 var authProps = new AuthenticationProperties
 {
      IsPersistent = isPersistent,
      ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(5)
 };
 await _signInManager.SignInAsync(user, authProps, authenticationMethod);

1 个答案:

答案 0 :(得分:0)

找不到通过authenticationProperties的方法,但是下面的代码有效。需要重写SingInManager类的SignInWithClaimsAsync方法

public override async Task SignInWithClaimsAsync(ApplicationUser user, AuthenticationProperties authenticationProperties, System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> additionalClaims)
{
    if (authenticationProperties != null && authenticationProperties.IsPersistent)
    {
        authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30);
    }

    await base.SignInWithClaimsAsync(user, authenticationProperties, additionalClaims);
}