【问题标题】:Update logged Identity claims when add new claims添加新声明时更新记录的身份声明
【发布时间】:2022-01-22 21:30:18
【问题描述】:

我有一个带有 Angular 的 .net core web api 应用程序。我使用 AspNetCore Identity。我有一个带有 userId 和 value 列的声明表。从 Angular 站点调用 createnewuser 方法,它们调用注册用户方法。这里没有任何问题。

我想将新声明添加到 httpcontext current user claims 列表中。 我试过了,但对我没有帮助。

Added a claim to an existing HttpContext User

但是当使用新令牌重新进行身份验证时,我可以看到在 _httpContext.HttpContext.User.Claims 中添加了新声明。如何查看将新声明添加到当前身份 (_httpContext.HttpContext.User.Claims)?

我从 httpontext 获得了当前用户 ID 值。

var parentId = _httpContext.HttpContext.User.Claims?.FirstOrDefault(p => p.Type == "uid");
                     if (parentId != null && parentId.Value != null)
                     {
                        await _IdentityService.AddClaimAsync(new RegisterRequest()
                         {
                          "newclaim" = "new-claim-key",
                          userId = parentId.Value
                      });

【问题讨论】:

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


【解决方案1】:

我认为最好从 db 那里得到这样的记录:

var claimList = userManager.
GetClaimsAsync(await_userManager.GetUserAsync(_httpContext.HttpContext.User))

虽然这不是一个非常准确的方法,但它是最可靠的方法。这样就可以在db中获取最新的claim list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-04
    • 2017-12-08
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多