Und*_*ore 8 c# asp.net cookies asp.net-identity-2
我无法让ASP Identity按需刷新存储在cookie中的身份.
在Startup.Auth.cs文件中,cookie设置为重新生成,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<QuizSparkUserManager, QuizSparkUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: ((manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)),
getUserIdCallback: ((claimsIdentity) => int.Parse(claimsIdentity.GetUserId())))
}
});
Run Code Online (Sandbox Code Playgroud)
但是我无法弄清楚如何刷新User.Identity代码中的内容,即在我需要刷新时强制刷新身份cookie.
我希望能够以编程方式使用重新生成身份回调,这可能吗?
我的问题与此类似:如何使用Asp.Net Identity 2将用户添加到角色后使.AspNet.ApplicationCookie无效?
但是我想刷新而不是使cookie无效.
编辑
在查看链接的问题后,我尝试了以下(没有完整的错误处理):
IOwinContext context = Request.GetOwinContext();
QuizSparkSignInManager manager = context.Get<QuizSparkSignInManager>();
ClaimsIdentity newIdentity = manager.CreateUserIdentity(manager.UserManager.FindById(User.Identity.GetUserId<int>()));
AuthenticateResult authenticationContext =
await context.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);
if (authenticationContext != null)
{
context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(
newIdentity, authenticationContext.Properties);
}
bool first2 = User.IsInRole("Turtle");
Run Code Online (Sandbox Code Playgroud)
Edit2:但是用户似乎仍然没有刷新.在页面重新加载它们似乎刷新,我是正确的认为这是因为User.Identity cookie是请求的一部分,不能在代码中更改?
如果您尝试向已登录的用户添加新角色,则需要将用户签名.然后使用新角色创建新身份,并使用新身份为用户签名.这是更新cookie的唯一方法.
检查用户属性是否已更改的最佳位置是您已使用的回调:CookieAuthenticationProvider.OnValidateIdentity.像这样的东西.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
// other stuff
Provider = new CookieAuthenticationProvider
{
// this function is executed every http request and executed very early in the pipeline
// and here you have access to cookie properties and other low-level stuff.
// makes sense to have the invalidation here
OnValidateIdentity = async context =>
{
// invalidate user cookie if user's security stamp have changed
var invalidateBySecirityStamp = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager));
await invalidateBySecirityStamp.Invoke(context);
if (context.Identity == null || !context.Identity.IsAuthenticated)
{
return;
}
if(/*Need to update cookie*/)
{
// get user manager. It must be registered with OWIN
var userManager = context.OwinContext.GetUserManager<UserManager>();
var username = context.Identity.Name;
// get new user identity with updated properties
var updatedUser = await userManager.FindByNameAsync(username);
// updated identity from the new data in the user object
var newIdentity = updatedUser.GenerateUserIdentityAsync(manager);
// kill old cookie
context.OwinContext.Authentication.SignOut(context.Options.AuthenticationType);
// sign in again
var authenticationProperties = new AuthenticationProperties() { IsPersistent = context.Properties.IsPersistent };
context.OwinContext.Authentication.SignIn(authenticationProperties, newIdentity);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
免责声明 - 从未测试过,甚至没有尝试编译它.
也可以看到我的其他答案 供参考 - 几乎相同的代码片段,但不同的目标.
UPD:
关于问题的另一部分 - 如何检测角色更改:
我可以想办法 - 在用户记录上有另一个GUID.与SecurityStamp框架类似,但未被框架使用.叫它MySecurityStamp.登录时将MySecurityStampcookie的值添加为声明.在每个请求MySecurityStamp中将cookie中的值与数据库中的值进行比较.如果值不同 - 重新生成标识的时间.并且在添加/删除的每个新角色中修改MySecurityStamp数据库中的用户.这将涵盖所有浏览器中的所有会话.
| 归档时间: |
|
| 查看次数: |
9590 次 |
| 最近记录: |