【发布时间】:2017-07-07 02:35:13
【问题描述】:
我编写了如下代码,用于在用户订阅我的网站后刷新用户角色,如下所示:
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 重置为普通用户?
【问题讨论】:
-
您确定这是 cookie,并且在让他们访问这些功能之前没有检查身份验证吗?
-
@BillRuhl 是的 100% ,我在登录时设置了检查,在贝宝上检查订阅配置文件的状态是活跃的、暂停的还是取消的......但问题是,我发现有些用户从来没有从应用程序注销(有很多),这个cookie留在他们的浏览器上永不过期,他们取消了他们的订阅,如果他们从不重新登录,他们在我网站上的订阅基本上永远不会过期......
-
我不知道是否有可能通过 mvc 应用程序强制每个人的 cookie 在他们访问网站后过期......这将立即解决我的问题
-
可能设置 cookie 过期在浏览器会话结束时过期...但我不知道该怎么做
-
在我的一些 MVC 应用程序中,您可以在启动类中设置验证间隔,也就是超时。应该有一个您传递 CookieAuthenticationOptions 的 configureAuth 方法。其中一个选项是“Provider”,您分配一个新的“CookieAuthenticationProvider”,它具有“validateInterval”属性并将其设置为等于 TimeSpan
标签: asp.net asp.net-mvc cookies asp.net-identity setcookie