我有一个WebApi(DemoService)。我使用IdentityServer4保护它。如果我使用Bearer令牌请求Api,我的DemoService会发出一些请求,以确保我可以访问DemoService。
在默认情况下,我的DemoService仅针对一个IdentityServer4进行授权,并且everthing运行良好。是否可以使IdentityServer4的URL(192.168.178.20:5200)具有灵活性,以对第二个IdentityServer4进行授权?或者是否可以添加第二个IdentityServer4。
这是我的Startup.cs:
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();
}
}
}