gpt4 book ai didi

c# - ASP 身份 2.0 : Regenerate Identity

转载 作者:太空狗 更新时间:2023-10-29 18:29:00 26 4
gpt4 key购买 nike

我无法让 ASP 身份按需刷新其存储在 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())))
}
});

但是我不知道如何在代码中刷新 User.Identity 上的内容,即在我需要刷新时强制刷新身份 cookie。

我希望能够以编程方式使用重新生成身份回调,这可能吗?

我的问题与此类似:How to invalidate .AspNet.ApplicationCookie after Adding user to Role using Asp.Net Identity 2?

但是我想刷新而不是使 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");

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);
}
}
}
});

免责声明 - 从未测试过,甚至没有尝试编译它。

也可以see my other answer供引用 - 几乎相同的一段代码,但目标不同。

更新:关于问题的另一部分——如何检测角色变化:
我可以想到一种方法 - 在用户记录上有另一个 GUID。类似于SecurityStamp,但不被框架使用。将其命名为 MySecurityStamp。在登录时将 MySecurityStamp 的值作为声明添加到 cookie。在每次请求时,将 cookie 中 MySecurityStamp 的值与数据库中的值进行比较。如果值不同 - 是时候重新生成身份了。在添加/删除每个新角色时,为数据库中的用户修改 MySecurityStamp。这将涵盖所有浏览器中的所有 session 。

关于c# - ASP 身份 2.0 : Regenerate Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com