我使用Asp.Net身份来控制我的应用程序的授权.现在,我需要这样做:如果用户在30分钟内没有操作,跳转到登录页面,当他登录时,没有选中"isPersistent"复选框.
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);
}
}
但我发现,当用户没有 Select iPersistent复选框时,cookies的过期日期已经是"会话",而不是当前时间加30分钟.
当使用类似after的代码时,cookies的状态会改变,因此"记住我"复选框无法工作:(.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromMinutes(30),
SlidingExpiration = true,
CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME
});