【问题标题】:Getting DiscoveryClient fails with "Issuer name does not match authority"获取 DiscoveryClient 失败并显示“颁发者名称与权限不匹配”
【发布时间】:2017-04-26 06:50:15
【问题描述】:

使用 IdentityModel 的DiscoveryClient 执行 GET 时出现以下错误,如下所示:

var discoveryResponse = await DiscoveryClient.GetAsync("https://localhost/IdentityServer");

颁发者名称与权限不匹配:https://localhost/identityserver

目标 URL 是在启用了 IdentityServer4 的 IIS 上运行的 ASP.NET Core Web 应用程序。客户端应用程序是在同一台机器上运行的经典 ASP.NET Web 应用程序。

显然,GET 确实设法从 IdentityServer 检索值,这由 discoveryResponse.Raw 的内容证明:

{
  "issuer": "https://localhost/identityserver",
  "jwks_uri": "https://localhost/IdentityServer/.well-known/openid-configuration/jwks",
  "authorization_endpoint": "https://localhost/IdentityServer/connect/authorize",
  "token_endpoint": "https://localhost/IdentityServer/connect/token",
  "userinfo_endpoint": "https://localhost/IdentityServer/connect/userinfo",
  "end_session_endpoint": "https://localhost/IdentityServer/connect/endsession",
  "check_session_iframe": "https://localhost/IdentityServer/connect/checksession",
  "revocation_endpoint": "https://localhost/IdentityServer/connect/revocation",
  "introspection_endpoint": "https://localhost/IdentityServer/connect/introspect",
  "frontchannel_logout_supported": true,
  "frontchannel_logout_session_supported": true,
  "scopes_supported": [ "CustomIdentityResources", "profile", "openid", "MyAPI.full_access", "offline_access" ],
  "claims_supported": [],
  "grant_types_supported": [ "authorization_code", "client_credentials", "refresh_token", "implicit" ],
  "response_types_supported": [ "code", "token", "id_token", "id_token token", "code id_token", "code token", "code id_token token" ],
  "response_modes_supported": [ "form_post", "query", "fragment" ],
  "token_endpoint_auth_methods_supported": [ "client_secret_basic", "client_secret_post" ],
  "subject_types_supported": [ "public" ],
  "id_token_signing_alg_values_supported": [ "RS256" ],
  "code_challenge_methods_supported": [ "plain", "S256" ]
}

【问题讨论】:

    标签: asp.net asp.net-core identityserver4


    【解决方案1】:

    授权:https://localhost/IdentityServer 发行人:https://localhost/identityserver

    它们不匹配 - 区分大小写。

    【讨论】:

    • 有趣。我无法追踪issuer 字段的来源。这是否源自托管应用程序的上下文?例如。如果我在名为“IdentityProvider”的应用程序中将 IdenityServer 端点托管在 IIS 中,那么颁发者将是 https://localhost/identityprovider(小写)?
    • IIRC - 发行人名称始终全部小写(以避免这些问题)。
    • 使用小写当然可以解决问题。谢谢!
    • @SigurdGarshol 你能分享一下你是如何让你的https 端点与 IS4 一起工作的吗?我无法让RequestClientCredentialsAsynchttps 合作
    • @Ruskin:在不知道您遇到什么样的错误的情况下很难回答。如果是“你是如何启用 https”的问题,那么我们就是这样做的:我们告诉 WebHostBuilder 将 Kestrel 与 Https 一起使用,并提供从文件中读取的自签名证书。这仅适用于我们将 IS4 端点作为控制台主机运行时 - 在“正确的”IIS 下运行时,我们将 https 和证书留给 Web 服务器。
    【解决方案2】:

    如果您无法更改服务器代码以适应策略,您可以更改策略设置以允许名称不匹配。

    例如,我尝试在 Azure Rest API 上使用 DiscoveryClientissuerhttps://sts.windows.net/{{ tenant_id }},而端点都以 https://login.microsoft.com/{{ tenant_id }} 开头。

    只需将字段 ValidateIssuerNameValidateEndpoints 设置为 false。

    var tenant_id = "8481D2AC-893F-4454-8A3B-A0297D301278"; // Made up for this example
    var authority = $"https://login.microsoftonline.com/{tenant_id}";
    DiscoveryClient discoveryClient = new DiscoveryClient(authority);
    
    // Accept the configuration even if the issuer and endpoints don't match
    discoveryClient.Policy.ValidateIssuerName = false;
    discoveryClient.Policy.ValidateEndpoints = false;
    
    var discoResponse = await discoveryClient.GetAsync();
    


    稍后编辑

    自发布此消息以来,DiscoveryClient 类已被弃用。

    这是新的调用语法:

    var client = new HttpClient();
    var discoResponse = await client.GetDiscoveryDocumentAsync(
        new DiscoveryDocumentRequest
        {
            Address = authority,
            Policy =
            {
                ValidateIssuerName = false,
                ValidateEndpoints = false,
            },
        }
    );
    

    【讨论】:

      【解决方案3】:

      其他答案针对客户端 - 使其接受小写发行者。

      这会改变发现文档中发行者的大小写:

      默认情况下,Identity Server 似乎将颁发者 Uri 更改为小写。这导致发行人的发现文档具有小写;以及您为其他所有内容输入代码/发布的案例。

      我在我的 Identity Server 应用、启动、ConfigureServices 方法中修复了这个问题

                  var builder = services.AddIdentityServer(options => { options.LowerCaseIssuerUri = false; })
      

      使用这意味着发现文档中颁发者的情况与所有其他 Uris 的情况相同

      【讨论】:

        猜你喜欢
        • 2019-09-20
        • 2015-10-10
        • 2020-09-21
        • 1970-01-01
        • 2019-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多