在调试模式下未获取 DotNetCore 授权策略

DotNetCore Authorization Policy not being picked up in debug mode

我在 startup.cs

中配置了身份验证策略
services.AddAuthorization(auth =>
        {
            auth.AddPolicy("Bearer", policy => policy
                .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​)
                .RequireAuthenticatedUser().Build());

        });

我在 api 控制器的属性中使用它:

[Authorize("Bearer")]

当我在 VS 代码中 运行 调试器时,出现异常: fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HL14EICAK26S": An unhandled exception was thrown by the application. System.InvalidOperationException: The AuthorizationPolicy named: 'Bearer' was not found. at Microsoft.AspNetCore.Authorization.AuthorizationPolicy.<CombineAsync>d__9.MoveNext()

当我通过 dotnet run 运行 程序时,我没有得到这个异常,它能够获取授权属性。

为什么会这样?

清理、恢复并重建了项目……它再次神奇地工作了……

我需要添加以下内容才能让所有内容在我的项目中正常工作。这里是Startup.cs

//Authentication
services.AddAuthentication()
    .AddScheme<AuthOptions1, AuthenticationHandler1>("Scheme1", opt => { })
    .AddScheme<AuthOptions2, AuthenticationHandler2>("Scheme2", opt => { });

//Authorization
services.AddAuthorization(opt =>
{
    opt.AddPolicy("Policy1", policy =>
    {
        policy.AddAuthenticationSchemes("Scheme1");
        policy.RequireAuthenticatedUser();
    });
    opt.AddPolicy("Policy2", policy =>
    {
        policy.AddAuthenticationSchemes("Scheme2");
        policy.RequireAuthenticatedUser();
    });
});

然后在我的控制器中我添加了这个:

[Authorize("Policy1")]