gpt4 book ai didi

asp.net-core - 从用户管理器获取 ClaimsPrincipal

转载 作者:行者123 更新时间:2023-12-03 18:35:50 24 4
gpt4 key购买 nike

在 ASP.NET Core Controller 中,我可以访问 User获取所有当前用户声明为 ClaimsPrincipal .现在我使用 userManager.CreateAsync 添加了一个新用户,添加了一些声明并需要他的所有声明作为其他功能的 ClaimsPrincipal。
我尝试了以下方法:

  var user1 = new IdentityUserEntity
{
// set username and stuff
};
var result = await userManager.CreateAsync(user1, passwort);
await userManager.AddClaimAsync(user1, new System.Security.Claims.Claim(MyClaimTypes.Stuff, MyClaimValues.Whatever));

之后我创建了一个这样的 ClaimsPrincipal :
  var user1cp = new ClaimsPrincipal(new ClaimsIdentity(await userManager.GetClaimsAsync(user1)));

问题是,这个 ClaimsPrincipal 不包含 NameIdentifier(也许还有其他东西?)。

如何从我刚添加到数据库的用户那里获得完整的 ClaimsPrincipal?

最佳答案

我认为您正在寻找 CreateUserPrincipalAsync SignInManager的方法:

/// <summary>
/// Creates a <see cref="ClaimsPrincipal"/> for the specified <paramref name="user"/>, as an asynchronous operation.
/// </summary>
/// <param name="user">The user to create a <see cref="ClaimsPrincipal"/> for.</param>
/// <returns>The task object representing the asynchronous operation, containing the ClaimsPrincipal for the specified user.</returns>
public virtual async Task<ClaimsPrincipal> CreateUserPrincipalAsync(TUser user) => await ClaimsFactory.CreateAsync(user);

关于asp.net-core - 从用户管理器获取 ClaimsPrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42611149/

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