Authentication 将web api身份验证从.NET Core 1.1迁移到2.0

Authentication 将web api身份验证从.NET Core 1.1迁移到2.0,authentication,asp.net-core,firebase-authentication,Authentication,Asp.net Core,Firebase Authentication,我正在尝试将旧的身份验证转换为.NET 2.0。我有以下代码: app.UseJwtBearerAuthentication(new JwtBearerOptions { AutomaticAuthenticate = true, IncludeErrorDetails = true, Authority = "https://securetoken.google.com/xxxxx", TokenValidationParameters = new TokenV

我正在尝试将旧的身份验证转换为.NET 2.0。我有以下代码:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    IncludeErrorDetails = true,
    Authority = "https://securetoken.google.com/xxxxx",
    TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidIssuer = "https://securetoken.google.com/xxxxxx",
        ValidateAudience = true,
        ValidAudience = "xxxx",
        ValidateLifetime = true,
    },
});
我的新代码如下:

public void Configure(...)
{
    ...
    app.UseAuthentication();
    ...
}

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.RequireHttpsMetadata = false;
            options.IncludeErrorDetails = true;
            options.Authority = "https://securetoken.google.com/xxxxxx";
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidIssuer = "https://securetoken.google.com/xxxxx",
                ValidateAudience = true,
                ValidAudience = "xxxxxx",
                ValidateLifetime = true,
            };
        }); 
    ...
    services.AddMvc();
    services.AddAuthorization(......);
}
但在2.0中,我得到了404响应。如果我从端点中删除
[Authorize]
属性,它就会工作。“我的输出”窗口显示以下内容:

Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 正在启动HTTP/1.1get
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息: 用户的授权失败:(null)。 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息: 对筛选器上的请求进行授权失败 “Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter”。 Microsoft.AspNetCore.Mvc.ChallengeResult:信息:正在执行 具有身份验证方案()的ChallengeResult

Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息: AuthenticationScheme:标识。应用程序被质询。 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息: 执行的动作 sorter.API.ContentManager.Controllers.userscocontroller.Info (Sorter.API.ContentManager)24.0837毫秒 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 完成时为35.2446ms 302 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 启动HTTP/1.1get
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 完成时间为5.8149ms 404

从日志错误来看,它似乎试图将我重定向到
/Account/Login
,但我没有这样的端点,我的项目是一个Web API


我是否缺少一些配置?

在阅读之前,我一直面临着同样的问题

当我们使用Authorize属性时,默认情况下它实际上绑定到第一个身份验证系统

解决方案是使用的特定方案(JWTBear):


现在我可以获得状态200(带有效令牌)和401(未授权-无效令牌)

谢谢!我被这个问题困扰了将近一个星期。现在我的变压器不工作,但这是另一个问题…哇。哇!谢谢。也非常感谢,自从更新到asp.net core 2.0以来,面临着同样的问题!
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "PoliceName")]