Search code examples
c#asp.net-coreidentityserver4asp.net-core-identityasp.net-core-3.1

How to change cookie expiration time when signin with PasswordSignInAsync?


How I can update Cookie Expiration time when signin with below code

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

Like can use with SingInAsyn

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

Solution

  • Didn't found the way to pass authenticationProperties but below code works. Need to override the SignInWithClaimsAsync method of SingInManager class

    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);
    }