【问题标题】:Replace discovery document endpoints in OpenIdConnectOptions Configuration without breaking the authorization flow (OpenID Connect .Net Core 3.1)在不中断授权流程的情况下替换 OpenIdConnectOptions 配置中的发现文档端点 (OpenID Connect .Net Core 3.1)
【发布时间】:2021-11-11 08:18:20
【问题描述】:

如何在不中断授权流程的情况下替换 OpenIdConnectOptions 配置对象中的发现文档端点?

startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
   .AddCookie()
   .AddOpenIdConnect(SetOpenIdConnectOptions)
   ;
}

private void SetOpenIdConnectOptions(OpenIdConnectOptions options)
{
    options.ClientId = Configuration["ClientId"];
    options.ClientSecret = Configuration["ClientSecret"];
    options.ResponseType = "code";
    options.ResponseMode = "query";

    //Option 1 - Set authority aka isssuer if discovery doc at default path e.g.https://someserver/.well-known/openid-configuration 
    //options.Authority = Configuration["AuthorityAddress"];

    //Option 2 - Explicitly set discovery document
    //options.MetadataAddress = "https://someserver/.well-known/openid-configuration"; 

    //Option 3 - Use configuration object and set URLs manually 
    var issuer = configuration["authorityaddress"];
    var authorization_endpoint = issuer + "/someidentityserver/authorize";
    var token_endpoint = issuer + "/someidentityserver/token";
    var userinfo_endpoint = issuer + "/someidentityserver/userinfo";
    var jwks_uri = issuer + "/someidentityserver/jwks";

    options.Configuration = new OpenIdConnectConfiguration()
    {
        AuthorizationEndpoint = authorization_endpoint,
        JwksUri = jwks_uri,
        TokenEndpoint = token_endpoint,
        UserInfoEndpoint = userinfo_endpoint,
        Issuer = issuer,
    };
}

上面的选项 1 和 2 完美运行:我单击一个私人页面并可以成功登录到我的身份服务器,但是如果我将它们注释掉并尝试手动配置端点,如选项 3 所示,我会收到以下错误:

SecurityTokenSignatureKeyNotFoundException:IDX10501:签名 验证失败。无法匹配键:孩子:'XXXXX'。

孩子在那里,可以在 jwks_uri json 中看到。

我被重定向到我的身份服务器,但登录后,我被重定向到我的应用程序,然后出现错误。

我还缺少什么进一步的配置?我需要向新的 OpenIdConnectConfiguration 对象添加更多内容吗?

当只定义一个发现文档时,.Net 框架可以清楚地处理这个问题,那么我怎样才能触发相同的功能呢?

非常感谢。

【问题讨论】:

    标签: c# .net-core authorization openid-connect


    【解决方案1】:

    当您收到此错误时:

    SecurityTokenSignatureKeyNotFoundException: IDX10501: Signature validation failed. Unable to match key: kid: 'XXXXX'.
    

    这通常是因为 IdentityServer 中的签名密钥已更改,而这可能在您重新部署 IdentityServer 时发生。因此,您需要做的是创建一个签名密钥并将其保存在 IdentityServer 之外的某个介质中。

    详情请参阅link

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2023-03-05
      • 2018-05-20
      • 1970-01-01
      • 2015-08-10
      • 2020-05-08
      • 2017-06-28
      • 2022-10-05
      • 2018-04-16
      相关资源
      最近更新 更多