【问题标题】:Why should I define JwtBearerDefaults.AuthenticationScheme for my Authorize attribute always?为什么我应该始终为我的 Authorize 属性定义 JwtBearerDefaults.AuthenticationScheme?
【发布时间】:2021-06-30 12:36:17
【问题描述】:

我有一个简单的 JWT 身份验证示例,您可以找到它here

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>

如您所见,我已将JwtBearerDefaults.AuthenticationScheme 添加到Startup/ConfigureServices 方法内的Authentication,因此我应该能够使用[Authorize] 独立,如下所示

[Authorize]
public sealed class WeatherForecastController : BaseController
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

但我不知道为什么它不起作用! (我已经用 Postman 测试过)。我必须通过AuthenticationSchemes定义它。

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public sealed class WeatherForecastController : BaseController
{
    private static readonly string[] Summaries = new[]
    {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

谁能指导我如何使用Authorize 独立属性没有 AuthenticationSchemes,尤其是当我在ConfigureServices 中定义它时?我设置错了什么?

【问题讨论】:

  • 我认为你应该 1:ConfigureServices 方法中的 services.AddAuthorization(),而不是 2:Configure 方法中的 app.UseAuthorization(),以便使用 ASP.Net 的授权功能,之后您应该在不指定方案的情况下获得授权

标签: c# asp.net-core jwt asp.net-identity


【解决方案1】:

您的代码不包含app.UseAuthentication()。这意味着没有身份验证中间件。

默认身份验证方案仅由身份验证中间件用于自动身份验证请求并建立当前主体。当您单独使用[Authorize] 时,不会设置身份验证方案并且授权过滤器将不起作用。从 IAuthorizeData(AuthorizeAttribute 是 IAuthorizeData)构建的过滤器是 AuthorizeFilter,这需要明确设置方案。

这就是为什么带有一些方案集的另一个[Authorize(...)] 可以工作,但[Authorize] 却不行。

所以你应该使用认证中间件来方便地使用[Authorize],而不必指定方案。

AuthorizeFilter 将在内部使用IPolicyEvaluator 来验证请求,这是PolicyEvaluator.AuthenticateAsync 的默认实现 - 你可以看到它检查AuthenticationSchemes 的可用性。

【讨论】:

  • 或者,如果没有为端点指定其他策略或要求,您还可以配置将由授权中间件使用的FallbackPolicy。
【解决方案2】:

在 .NET 5 中,您可以进行如下配置:

在AddJwtBearer

中添加JwtBearerDefaults.AuthenticationScheme作为默认方案字符串
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
      .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwt =>
       {
         jwt.TokenValidationParameters = new TokenValidationParameters
           {
             ValidateIssuer = true,
                       ....
            ;
                
         });

在配置中使用(在使用角色的情况下)

  app.UseAuthentication();
  app.UseAuthorization();

然后您可以使用 [Authorired] 属性作为默认值

[ApiController]
[Route("api/data")]
[Authorize]
public class DataController : ControllerBase

【讨论】:

    【解决方案3】:

    AddIdentityCore() 和 AddIdentity() 方法之间也存在冲突。如果您使用 AddIdentity,它使用基于 Cookie 的身份验证,因此如果您不明确编写 [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)],JWT 将不起作用。我将我的启动更改如下,现在它可以与 .Net 6 中的 [Authorize] 一起使用。我正在为用户和角色使用自定义类。

    services.AddIdentityCore<AppUser>(x =>
    {
        x.Password.RequireDigit = false;
        x.Password.RequiredLength = 4;
        x.Password.RequireLowercase = false;
        x.Password.RequireNonAlphanumeric = false;
        x.Password.RequireUppercase = false;
    })
    .AddRoles<AppRole>()
    .AddEntityFrameworkStores<DBContext>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2020-11-03
      • 2017-08-27
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多