【问题标题】:Asp.Net Core jwt token is transformed after authenticationAsp.Net Core jwt token 认证后转换
【发布时间】:2022-01-19 18:58:25
【问题描述】:

我有 email 声明的 Cognito id 令牌。

  ..........
  "iat": 164456734,
  "jti": "81ac2634-e241-444f-88cf-eabf454644",
  "email": "david@mail.com"
}

但是,在 c# 中,asp net core jwt 中间件身份验证电子邮件声明从email 类型转换为http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress - ClaimTypes.Email

但后来我手动读取了令牌:

var token = new JwtSecurityTokenHandler().ReadJwtToken(jwtToken);
var claimsIdentity = new ClaimsIdentity(token.Claims);
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity)

声明类型未转换并保持为email

为什么在 asp net core 中的身份验证声明被转换为http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress

我可以在不手动修改Claims 列表的情况下手动创建claimsPrincipal 来进行email 声明转换吗?

【问题讨论】:

    标签: asp.net-core jwt openid-connect


    【解决方案1】:

    其实已经理解为ClaimTypes.Email,但是这个属性返回的字符串是http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddresssource)。

    除非特别这样做,否则不会转换令牌,而是将其解析并理解为ClaimTypes.Email,而实际令牌不会被修改。

    【讨论】:

    【解决方案2】:

    因此,Microsoft 和 OpenIDConnect 对电子邮件声明名称应该是什么有不同的看法,要禁用此重新映射,您可以执行以下任一操作:

    public void ConfigureServices(IServiceCollection services)
    {
        // By default, Microsoft has some legacy claim mapping that converts
        // standard JWT claims into proprietary ones. This removes those mappings.
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
        JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();
    
    
    
        // Or set this flag to false
        .AddJwtBearer(opt =>
        {
            ...
            opt.MapInboundClaims = false;
        });
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2018-12-22
      • 2017-11-02
      • 2020-08-08
      • 2019-10-18
      • 2019-09-26
      • 2020-10-21
      • 1970-01-01
      • 2017-10-25
      相关资源
      最近更新 更多