Rey*_*gle 5 jwt asp.net-core identityserver4
我有 4 个身份验证服务器,用于验证我的应用程序上传入请求的令牌。
我有以下ConfigureServices
配置Startup.cs
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();
});
Run Code Online (Sandbox Code Playgroud)
当我调用 API 时,它工作正常。
问题是假设任何身份验证服务器出现故障,并且我尝试调用 API,然后应用程序会给出错误,指出未找到特定的身份验证服务器或任何特定于该情况的错误。
1)如何跳过任何身份验证服务器出现故障时可能发生的错误?
2)在选择相应的身份验证服务器来验证传入请求时,策略如何工作?它的工作方式是否像 switch case(直接跳转到相应的身份验证服务器)或 if-elseladder(检查每个身份验证服务器的请求验证,直到找到实际的服务器)
我已经实现了我在问题中所要求的。
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;
},
};
});
Run Code Online (Sandbox Code Playgroud)
2)在选择相应的身份验证服务器来验证传入请求时,策略如何工作?它的工作方式是否像 switch case(直接跳转到相应的身份验证服务器)或 if-elseladder(检查每个身份验证服务器的请求验证,直到找到实际的服务器)
这看起来就像 if-else 梯子一样。我在里面记录了一些信息OnAuthenticationFailed
,我发现即使只有 1 个身份验证服务器应该处理该请求,但所有其他身份验证服务器也在尝试处理它并失败。
归档时间: |
|
查看次数: |
1030 次 |
最近记录: |