更新时间:2023-02-25 09:17:43
我通过从SignInManager继承并添加新属性成功实现了这一点:
I succeeded to achieve this by inheriting from SignInManager, adding a new property:
/// <summary>
/// Defines how long a user session lasts if it is non persistent. A null value means until the browser is closed (default)
/// </summary>
public virtual TimeSpan? NonPersistentSessionDuration { get; set; }
并覆盖方法:
/// <summary>
/// Creates a user identity and then signs the identity using the AuthenticationManager
/// </summary>
/// <param name="user"></param>
/// <param name="isPersistent"></param>
/// <param name="rememberBrowser"></param>
/// <returns></returns>
public virtual async Task SignInAsync(TUser user, bool isPersistent, bool rememberBrowser)
{
var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture();
// Clear any partial cookies from external or two factor partial sign ins
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
var properties = new AuthenticationProperties {IsPersistent = isPersistent};
if (!isPersistent && this.NonPersistentSessionDuration != null)
properties.ExpiresUtc = DateTimeOffset.UtcNow.Add(this.NonPersistentSessionDuration.Value);
if (rememberBrowser)
{
var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id));
AuthenticationManager.SignIn(properties, userIdentity, rememberBrowserIdentity);
}
else
{
AuthenticationManager.SignIn(properties, userIdentity);
}
}
此更改是将AuthenticationProperties对象的ExpiresUtc属性设置为非持久登录.
The change is setting the ExpiresUtc property of the AuthenticationProperties object for non-persistent sign in.
当然,在SignInManager配置中,您必须将新的NonPersistentSessionDuration属性设置为某个值,该值将是非持久会话的会话长度.
Of course in the SignInManager configuration you have to set the new NonPersistentSessionDuration property to some value which will be the session length for non-persistent sessions.