【问题标题】:Identity Server 4 Failed to Validate the Token from localhost for the protected APIIdentity Server 4 无法验证来自 localhost 的受保护 API 的令牌
【发布时间】:2018-09-14 17:31:04
【问题描述】:

场景

  • localhost & 192.168.0.10 指的是同一台名为 MyPC 的机器
  • 身份服务器正在该“MyPC”上运行
  • UI 使用“localhost”与 Identity Server 通信
  • 受保护的 API 使用“192.168.0.10”与 Identity Server 通信
  • UI 和 API 相互通信并由 Identity Server 进行身份验证

在上述场景中,如果我使用 Identity Server 3,一切都很好;但是,当我切换到 Identity Server 4(最新版本)时,问题就开始了。我在 API 中遇到了这个错误

System.IdentityModel.Tokens.SecurityTokenInvalidIssuerException:IDX10205:颁发者验证失败。发行者:'localhost:9987'......

如果我为 UI 和 API 使用相同的 IP 地址(localhost 或 192.168.0.10)与身份服务器通信,该错误就会消失。

谁能给我指出解决这个问题的方向?我确实需要能够使用 127.0.0.1、localhost、Lan 地址、Wan 地址访问 Identity Server?

这是我在 API 启动中使用的代码:

services.AddAuthentication(o =>
            {
                o.DefaultScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
                o.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
            })
            .AddIdentityServerAuthentication(x =>
            {
                x.Authority = Configuration.GetSection("URL").GetSection("IdentityServerURL").Value;
                x.ApiSecret = Configuration.GetSection("Resource").GetSection("APISecrets").Value;
                x.ApiName = Configuration.GetSection("Resource").GetSection("APIName").Value;
                x.SupportedTokens = SupportedTokens.Both;
                x.RequireHttpsMetadata = false;
            });

P/S:我对此进行了研究,但没有任何明确的答案。

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    IdentityServer4 服务器将在 JWT 令牌中包含一个“Issuer”。您可以仔细检查,例如使用jwt.io 那是什么。来自the relevant docs

    设置将出现在发现文档中的颁发者名称和颁发的 JWT 令牌。建议不要设置此属性,它会根据客户端使用的主机名推断颁发者名称。

    如果您进行自定义命名、别名、端口、子域等等,那么您可以继续手动设置(无论是在服务器端还是在验证端),或者关闭颁发者验证(可能仅在dev) 完全一致。

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 1970-01-01
      • 2020-12-07
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多