【问题标题】:How to get user claims by using JWT Bearer token如何使用 JWT Bearer 令牌获取用户声明
【发布时间】:2019-06-22 16:29:23
【问题描述】:

我正在从 Postman 的标头中发送 Bearer Token。现在我需要使用该不记名令牌在 API 中获取用户声明。我尝试的代码不起作用意味着没有获取用户名/电子邮件。如何使用 Bearer Token 获取用户声明?

public class RepositoryUserAccount : IRepositoryUserAccount
{
    private readonly HttpContext _httpContext;

    public RepositoryUserAccount(IHttpContextAccessor httpContextAccessor)
    {
        this._httpContext = httpContextAccessor.HttpContext;
    }

    public async Task EnableAuthenticator()
    {
        ClaimsPrincipal currentUser = _httpContext.User;
        var currentUserName = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
    }
}

【问题讨论】:

  • @RayanWilson 我的意思是没有得到像姓名/电子邮件这样的用户声明......
  • 这是怎么注册的?单例还是传递?您的身份验证提供程序设置为什么?
  • @Daniel Singleton。身份验证提供程序设置????
  • _httpContext 将为空或只是第一个请求命中。将其更改为在方法中使用访问器。
  • @Chandan Y S 您在登录用户时是否在令牌中设置用户声明?

标签: c# claims-based-identity httpcontext bearer-token


【解决方案1】:

假设System.Security.Claims.ClaimTypes 是身份验证

var claims = _httpContext.User.Claims.Where(x => x.Type == ClaimTypes.Authentication);
foreach (var claim in claims)
{
    var value = claim.Value; //<= the value of each claim
}

【讨论】:

    【解决方案2】:

    如您的评论所述,您正在将其注册为单例。重构此类以每次使用IHttpContextAccessor 或将其更改为传递依赖项。

    这是第一种方法:

    public class RepositoryUserAccount : IRepositoryUserAccount
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
    
        public RepositoryUserAccount(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
    
        public async Task EnableAuthenticator()
        {
            ClaimsPrincipal currentUser = _httpContextAccessor.HttpContext.User;
            var currentUserName = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
        }
    }
    

    【讨论】:

      【解决方案3】:

      我所做的是,在启动时我配置了类似的服务,

      var key = Encoding.ASCII.GetBytes(SECRET_KEY);
                  services.AddAuthentication(x =>
                  {
                      x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                      x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                  })
                  .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, x =>
                  {
                      x.RequireHttpsMetadata = false;
                      x.SaveToken = true;
                      x.TokenValidationParameters = new TokenValidationParameters
                      {
                          ValidateIssuerSigningKey = true,
                          IssuerSigningKey = new SymmetricSecurityKey(key),
                          ValidateIssuer = false,
                          ValidateAudience = false
                      };
                  });
      

      在我喜欢的控制器中,

      [HttpGet]
      [Route("EnableAuthenticator")]
      public void EnableAuthenticator()
      {
         var user = HttpContext.User;
      }
      

      在用户中,我们将获得所有索赔。就是这样!

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 2016-04-30
        • 2019-11-07
        • 2017-02-13
        • 1970-01-01
        • 2021-04-30
        • 2020-11-29
        相关资源
        最近更新 更多