【问题标题】:How to configure two scheme authentication in AspNetCore 2.0AspNetCore 2.0中如何配置两种方案认证
【发布时间】:2025-12-13 11:15:01
【问题描述】:

应用设置:

Startup.cs:

控制器:

我添加为设置,但不断收到授权错误,帮帮我?

【问题讨论】:

  • 你的错误信息是什么?
  • 请将您的代码粘贴为文本。图片更难搜索,因此更难回答

标签: c# asp.net api asp.net-core-2.0 bearer-token


【解决方案1】:

ConfigureServices(IServiceCollection services)中的Startup.cs:

services.AddAuthentication(o =>
{
    o.DefaultAuthenticateScheme = "Bearer";
})
.AddOAuthIntrospection("Bearer", o =>
{
    o.Authority = new Uri(Configuration["URL"]);
    o.Audiences.Add("Audiences");
    o.ClientId = Configuration["OpenIdConnectOptions:ClientId"];
    o.ClientSecret = Configuration["OpenIdConnectOptions:ClientSecret"];
}).AddOAuthIntrospection("Bearer2", o =>
{
    o.Authority = new Uri(Configuration["URL"]);
    o.Audiences.Add("Audiences");
    o.ClientId = Configuration["OpenIdConnectOptions:ClientId2"];
    o.ClientSecret = Configuration["OpenIdConnectOptions:ClientSecret2"];
});

全部在控制器上:

[Authorize(ActiveAuthenticationSchemes = "Bearer,Bearer2")]
[Route("[controller]")]
public class Controller : ControllerBase
{

【讨论】: