【问题标题】:Get ClaimsPrincipal from usermanager从用户管理器获取 ClaimsPrincipal
【发布时间】:2017-07-25 11:14:28
【问题描述】:

在 ASP.NET Core 控制器中,我可以访问 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?

【问题讨论】:

    标签: asp.net-core claims-based-identity


    【解决方案1】:

    我想你正在寻找SignInManagerCreateUserPrincipalAsync方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2010-11-26
      • 1970-01-01
      相关资源
      最近更新 更多