【发布时间】:2020-10-14 20:05:59
【问题描述】:
我使用 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
});
}
和这样的登录代码:
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的过期日期已经是'Session',而不是当前时间加上30分钟。
使用类似之后的代码时的cookie状态,因此“记住我”复选框不起作用。:(。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromMinutes(30),
SlidingExpiration = true,
CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME
});
【问题讨论】:
标签: c# asp.net cookies asp.net-identity