.Net Core - 具有多个身份验证服务器的身份服务器

.Net Core - Identity Server with Multiple Auth Servers

我有 4 个身份验证服务器,用于验证我的应用程序传入请求的令牌。

我在 Startup.cs

ConfigureServices 中有以下配置
services.AddAuthentication()
    .AddJwtBearer("authServer1", options =>
     {
         options.Authority = "https://authserver1.com/AuthServices/Auth";
         options.Audience = "web.api";
     })
    .AddJwtBearer("authServer2", options =>
    {
        options.Authority = "https://authserver2.com/AuthServices/Auth";
        options.Audience = "web.api";
    })
    .AddJwtBearer("authServer3", options =>
    {
        options.Authority = "https://authserver3.com/AuthServices/Auth";
        options.Audience = "web.api";
    })
    .AddJwtBearer("authServer4", options =>
    {
        options.Authority = "https://authserver4.com/AuthServices/Auth";
        options.Audience = "web.api";
    });

services.AddAuthorization(options =>
    {
        options.DefaultPolicy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .AddAuthenticationSchemes("authServer1", "authServer2", "authServer3", "authServer4")
            .Build();
    });

当我调用 API 时,它工作正常。

问题是假设任何 auth 服务器出现故障,我尝试调用 API 然后应用程序给出错误,指出未找到特定的 auth 服务器或特定情况的任何错误。

1) 如何跳过任何身份验证服务器出现故障时可能发生的错误?

2) 在选择相应的身份验证服务器以验证传入请求时,策略如何工作?它是否像 switch case(直接跳转到相应的 auth 服务器)或 if-else 阶梯(检查每个 auth 服务器的请求验证,直到找到实际的服务器)一样工作

我已经达到了我在问题中的要求。

1) 如何跳过任何身份验证服务器出现故障时可能发生的错误?

我配置了 OnAuthenticationFailed 来抑制请求失败的错误

.AddJwtBearer("authServer1", options =>
{
    options.Authority = "https://authServer1.com/AuthServices/Auth";
    options.Audience = "web.api";
    options.Events = new JwtBearerEvents()
    {
        OnAuthenticationFailed = (context) =>
        {
            context.NoResult();
            return Task.CompletedTask;
        },
    };
});

2) 在选择相应的身份验证服务器以验证传入请求时,策略如何工作?它是否像 switch case(直接跳转到相应的 auth 服务器)或 if-else 阶梯(检查每个 auth 服务器的请求验证,直到找到实际的服务器)一样工作

这看起来像 if-else 阶梯。我在 OnAuthenticationFailed 中记录了一些信息,我发现即使只有 1 个身份验证服务器应该处理该请求,但所有其他身份验证服务器也在尝试处理它但失败了。