AuthorizeAttribute 不会影响在 dotnet 核心 webapi 2.1 应用程序中使用 JWT 的授权

The AuthorizeAttribute doesn't effect authorization using JWT in dotnet core webapi 2.1 application

我正在尝试在核心 webapi 应用程序中使用 JWT。这是我的 Startup.ConfigureServices():

services.AddDbContextPool("Bla Bla");
services.AddIdentity<IdentityUser, IdentityRole>("Bla Bla");

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(o => {
    o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(c => {
    c.RequireHttpsMetadata = false;
    c.SaveToken = true;
    c.TokenValidationParameters = 
        new TokenValidationParameters {
            ValidIssuer = Configuration["JwtIssuer"],
            ValidAudience = Configuration["JwtIssuer"],
            IssuerSigningKey = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
            ClockSkew = TimeSpan.Zero
        };
});

services.AddRouting("Bla Bla");
services.AddMvcCore("Bla Bla").AddControllersAsServices();

这是Startup.Configure()方法:

app.UseAuthentication();
app.UseMvc(rb => 
    { rb.MapRoute("api_default", "{controller}/{action}/{id?}"); });

无论如何,我已经创建了一个保护操作来测试(我期望 return 一个 HTTP 401 Unauthorized 错误):

public class AccountController : ControllerBase {
    [HttpGet]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public async Task<object> Info() {
        return "Authorized!";
    }
}

但它一直在获得授权和响应 "Authorized!"。 Authorize 属性似乎根本不起作用。我错过了什么吗?

P.S。 [Authorize][Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] 属性我都试过了。

P.S.2 我尝试从 ControllerBaseController.

扩展控制器

P.S.3 我试过没有清除默认声明(删除了这一行 JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();)。

None 他们工作了。

框架和包信息:

更新:

根据 ,我已经 return 编辑了操作中的 User 对象:

[HttpGet]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public async Task<object> Info() {
    return User;
}

这是输出 JSON:

{
    "identities": [
        {
            "isAuthenticated": false,
            "claims": [],
            "roleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
            "nameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
        }
    ],
    "identity": {
        "isAuthenticated": false,
        "claims": [],
        "roleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
        "nameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
    },
    "claims": []
}

主要问题是,当您使用 .AddMvcCore() 时,您只是连接了使用 .AddMvc() 时获得的一小部分。对于许多解决方案,只使用后者更简单,因为您随后会自动添加以下内容并准备好使用 [source]:

  • API 资源管理器
  • 授权
  • 观看次数
  • Razor 视图引擎
  • 剃刀页面
  • JSON 格式化程序
  • CORS
  • (其他几个)

如果您希望只添加您绝对需要的内容,您可以选择使用 .AddMvcCore(),但您需要明确添加您需要的 MVC 服务;例如在这种情况下(授权),您需要

services.AddMvcCore()
        .AddAuthorization();

注意这里的 .AddAuthorization() 是如何应用到 services.AddMvcCore() 的结果的,它是 IMvcBuilder 而不是 services,这是一个 IServiceCollection.