如何设置asp.net标识cookie到期时间
我使用Asp.Net Identity来控制我的应用程序的授权。 现在,我需要这样做:如果用户在30分钟内没有运行,请跳转到登录页面,当他登录时不选择“isPersistent”复选框。 并且,如果他选择“isPersistent”复选框,请将Cookie的到期日期设置为14天。 我尝试通过像这样更改Startup.Auth.cs来做到这一点:
public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), SlidingExpiration = true, CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME }); }
和SignIn代码如下:
private async Task SignInAsync(User user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); if (isPersistent) { AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); } else { AuthenticationManager.SignIn(new AuthenticationProperties() { ExpiresUtc = new DateTimeOffset(DateTime.UtcNow.AddMinutes(30)) }, identity); } }
但我发现当用户没有选择isPersistent复选框时,cookies的到期日期已经是“会话”,而不是当前时间加上30分钟。
使用像之后的代码时的cookie状态,所以’记住我’复选框无法正常工作。:(。
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), ExpireTimeSpan = TimeSpan.FromMinutes(30), SlidingExpiration = true, CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME });
如果AuthenticationProperties
IsPersistent
属性设置为false,则cookie过期时间设置为Session。
如果选中复选框 “记住我”,则检查 AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true }, userIdentity);
将创建一个cookie,其过期时间等于您在Startup.cs
设置的ExpireTimeSpan
(默认为14天)。
如果未选中复选框 “记住我”,则必须使用AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30)}, userIdentity);
。 再次将IsPersistent
设置为true,但现在我们为ExpiresUtc赋值,因此它不会使用Startup.cs
CookieAuthenticationOptions
。
public override async Task SignInAsync(ApplicationUser 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); if (rememberBrowser) { var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id)); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity); } else { //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity); if (isPersistent) { AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity); } else { AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30) }, userIdentity); } } }
用这个…
public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(1), }); }
我遇到了同样的问题,这段代码对我有用(在Startup.cs文件中)
services.Configure(options => { options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(9999); });
这为持久性cookie增加了大约27年 (或永不过期)。
注意:如果你想要更少的到期时间,你可以使用TimeSpan.FromMinutes(1);
1分钟或TimeSpan.FromSeconds(30);
等30秒等..
根据@tmg的回答,以下解决方案只需在[HttpPost]Login()
动作中使用即可完美运行:
await SignInManager.PasswordSignInAsync(userName: model.Email, password: model.Password, isPersistent: !model.RememberMe, shouldLockout: true);
其中isPersistent: !model.RememberMe
(注意相反!
)给出, 正如人们所预料的那样 ,
expiration date = Session
如果用户选择RememberMe = true
则为expiration date = Session
如果用户选择RememberMe = false
则expiration date = ExpireTimeSpan
(例如15分钟)