Asp.NET 身份设置持久性 cookie 在特定时间后过期
Asp.NET Identity setting persistent cookie to expire after specific time
我编写了如下代码,在用户订阅我的网站后刷新用户角色,如下所示:
private void RefreshUserRoles()
{
var AuthenticationManager = HttpContext.GetOwinContext().Authentication;
var Identity = new ClaimsIdentity(User.Identity);
Identity.RemoveClaim(Identity.FindFirst(ClaimTypes.Role));
Identity.AddClaim(new Claim(ClaimTypes.Role, "Subscriber"));
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
new ClaimsPrincipal(Identity),
new AuthenticationProperties { IsPersistent = true}
);
}
请注意这行代码:
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
new ClaimsPrincipal(Identity),
new AuthenticationProperties { IsPersistent = true}
);
用户返回我的网站后,我将 cookie 设置为持久性,但我忘记为该 cookie 设置过期时间。我应该将此 cookie 设置为持续 30 分钟,之后应要求用户重新登录系统。
由于一些用户从不重新登录网站,这给我留下了一个问题,现在我需要在他们访问网站时重置所有用户浏览器中的 cookie,并在他们取消订阅时更改他们的角色。我注意到一些用户取消了他们的订阅并且从未重新登录,但他们仍然能够使用我网站上的功能...
所以我的问题是:
如何将 cookie 的有效期设置为 30 分钟,之后用户将被要求重新登录系统。
如何设置重新检查用户的 cookie,如果他们很长时间没有过期,以便我可以在他们取消订阅时将他们的 cookie 重置为普通用户?
这是我所说的 ConfigureAuth 方法:
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Controller/Action"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
我就是这样设置的,对我来说很管用。
我编写了如下代码,在用户订阅我的网站后刷新用户角色,如下所示:
private void RefreshUserRoles()
{
var AuthenticationManager = HttpContext.GetOwinContext().Authentication;
var Identity = new ClaimsIdentity(User.Identity);
Identity.RemoveClaim(Identity.FindFirst(ClaimTypes.Role));
Identity.AddClaim(new Claim(ClaimTypes.Role, "Subscriber"));
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
new ClaimsPrincipal(Identity),
new AuthenticationProperties { IsPersistent = true}
);
}
请注意这行代码:
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(
new ClaimsPrincipal(Identity),
new AuthenticationProperties { IsPersistent = true}
);
用户返回我的网站后,我将 cookie 设置为持久性,但我忘记为该 cookie 设置过期时间。我应该将此 cookie 设置为持续 30 分钟,之后应要求用户重新登录系统。
由于一些用户从不重新登录网站,这给我留下了一个问题,现在我需要在他们访问网站时重置所有用户浏览器中的 cookie,并在他们取消订阅时更改他们的角色。我注意到一些用户取消了他们的订阅并且从未重新登录,但他们仍然能够使用我网站上的功能...
所以我的问题是:
如何将 cookie 的有效期设置为 30 分钟,之后用户将被要求重新登录系统。
如何设置重新检查用户的 cookie,如果他们很长时间没有过期,以便我可以在他们取消订阅时将他们的 cookie 重置为普通用户?
这是我所说的 ConfigureAuth 方法:
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Controller/Action"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
我就是这样设置的,对我来说很管用。