C# 我可以将第二个IdentityServer4添加到我的WebApi身份验证管道吗?
我有一个WebApi(DemoService)。我用IdentityServer 4保护它。如果我使用承载令牌请求Api,我的DemoService会发出一些请求,以确保允许我访问DemoServiceC# 我可以将第二个IdentityServer4添加到我的WebApi身份验证管道吗?,c#,asp.net-core,identityserver4,C#,Asp.net Core,Identityserver4,我有一个WebApi(DemoService)。我用IdentityServer 4保护它。如果我使用承载令牌请求Api,我的DemoService会发出一些请求,以确保允许我访问DemoService 得到 得到 在默认情况下,我的DemoService只对一个IdentityServer4进行授权,并且一切正常。是否可以使IdentityServer 4的URL(192.168.178.20:5200)灵活,以便针对第二个IdentityServer 4进行授权?或者是否可以添加第二个标识服
namespace DemoService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
// can I decide in the current
// Request which Authority to use?
// I want to switch the url between two
// IdentityServers
options.Authority ="http://192.168.178.20:5200";
options.RequireHttpsMetadata = false;
options.ApiName = "DemoService";
});
//// If I try to add a second IdentityServer I
//// get the following failure:
//// System.InvalidOperationException: 'Scheme already exists: BearerIdentityServerAuthenticationJwt'
// services.AddAuthentication("Bearer")
// .AddIdentityServerAuthentication(options =>
// {
// options.Authority ="http://localhost:5000";
// options.RequireHttpsMetadata = false;
// options.ApiName = "DemoService";
// });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}
}
}
因此,您想针对两个不同的身份服务器进行身份验证?请尝试更改第二个方案的名称,错误消息只是告诉您不能以“承载者”的名称注册两个方案。顺便说一下,您指定的名称并不控制方案的使用方式。如果仍然不起作用,请尝试使用generic
AddOpenIdConnect
config而不是IdentityServer特定的配置来注册它们。@DalmTo:是的,用户应该可以选择根据两个不同的标识进行身份验证servers@McGuireV10你说得对。我可以在Bearer4中重命名Bearer,效果很好。如果我在管道中添加第二个IdentityServer4,它似乎试图针对错误的IdentityServer4(错误的孩子)进行身份验证。失败原因如下:Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException:'IDX10501:签名验证失败。无法匹配'kid':'4c301c179f1a2295f73d3050869b68cf',标记:“{”alg:“RS256”,“typ:“JWT”,“kid:“4c301c179f1a2295f73d3050869b68cf”}。{“注意……我没有答案,也没有时间设置一个全新的ID4服务器,但我很好奇为什么您需要两个服务器?这听起来像是我们所说的一个。