3

我在使用 JWT 生成的令牌中有以下有用的负载

{“子”:“flamelsoft@gmail.com”,“jti”:“0bca1034-f3ce-4f72-bd91-65c1a61924c4”,“ http://schemas.microsoft.com/ws/2008/06/identity/claims/角色“:“管理员”,“exp”:1509480891,“iss”:“ http://localhost:40528 ”,“aud”:“ http://localhost:40528 ”}

使用此代码 Startup.cs

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<DBContextSCM>(options =>
        options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), b =>
         b.MigrationsAssembly("FlamelsoftSCM")));

        services.AddIdentity<User, Role>()
            .AddEntityFrameworkStores<DBContextSCM>()
            .AddDefaultTokenProviders();

        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));

        services.AddAuthentication()
             .AddJwtBearer(cfg =>
             {
                 cfg.RequireHttpsMetadata = false;
                 cfg.SaveToken = true;

                 cfg.TokenValidationParameters = new TokenValidationParameters()
                 {
                     ValidIssuer = Configuration["Tokens:Issuer"],
                     ValidAudience = Configuration["Tokens:Issuer"],
                     IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
                 };

             });

        services.AddMvc();
    }

AccountController.cs

        [HttpPost]
    [Authorize(Roles="Administrator")]
    public async Task<IActionResult> Register([FromBody]RegisterModel model)
    {
        try
        {
            var user = new User { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var role = await _roleManager.FindByIdAsync(model.Role);
                result = await _userManager.AddToRoleAsync(user, role.Name);

                if (result.Succeeded)
                    return View(model);
            }
            return BadRequest($"Error: Could not create user");
        }
        catch (Exception ex)
        {
            return BadRequest($"Error: {ex.Message}");
        }
    }

用户服务.ts

export class UserService {

constructor(private http: Http, private config: AppConfig, private currentUser: User) { }

create(user: User) {
    return this.http.post(this.config.apiUrl + 'Account/Register', user, this.jwt());
}

private jwt() {
    const userJson = localStorage.getItem('currentUser');
    this.currentUser = userJson !== null ? JSON.parse(userJson) : new User();

    if (this.currentUser && this.currentUser.token) {
        let headers = new Headers({ 'Authorization': 'Bearer ' + this.currentUser.token });
        return new RequestOptions({ headers: headers });
    }
}}

问题是角色的验证不起作用,请求到达控制器并在标头中返回代码200,但从未进入类。当我删除 [Authorize (Roles = "Administrator")] 时,它会正确输入我的代码。有什么不好定义的吗?或者通过角色定义授权的替代方法是什么。

4

2 回答 2

5

TL;博士

如原始问题的评论中所述,更改:

[HttpPost]
[Authorize(Roles = "Administrator")]
public async Task<IActionResult> Register([FromBody]RegisterModel model)
{
    // Code
}

[HttpPost]
[Authorize(AuthenticationSchemes = "Bearer", Roles = "Administrator")]
public async Task<IActionResult> Register([FromBody]RegisterModel model)
{
    // Code
}

解决了这个问题。

Bearer是在 ASP.NET Core 中使用 JWT 不记名身份验证时的默认身份验证方案名称。


但是为什么我们需要在AuthenticationSchemes属性上指定[Authorize]属性呢?

这是因为配置身份验证方案并不意味着它们将在每个 HTTP 请求上运行。如果匿名用户可以访问特定操作,为什么还要从 cookie 或令牌中提取用户信息?MVC 对此很聪明,并且只会在需要时运行身份验证处理程序,即在以某种方式受到保护的请求期间。

在我们的例子中,MVC 发现了[Authorize]属性,因此知道它必须运行身份验证和授权来确定请求是否被授权。诀窍在于它只会运行已指定的身份验证方案处理程序。在这里,我们没有,所以没有执行身份验证,这意味着授权失败,因为请求被认为是匿名的。

将身份验证方案添加到属性指示 MVC 运行该处理程序,该处理程序从 HTTP 请求中的令牌中提取用户信息,从而导致Administrator发现角色,并允许请求。


作为旁注,还有另一种方法可以实现这一点,而无需使用AuthenticationSchemes属性的[Authorize]属性。

AuthenticationSchemes想象一下,您的应用程序只配置了一个身份验证方案,必须在每个属性上指定该属性会很痛苦[Authorize]

使用 ASP.NET Core,您可以配置默认身份验证方案。这样做意味着将为每个 HTTP 请求运行相关的处理程序,而不管资源是否受到保护。

设置分为两部分:

public class Startup
{
    public void ConfiguresServices(IServiceCollection services)
    {
        services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme /* this sets the default authentication scheme */)
            .AddJwtBearer(options =>
            {
                // Configure options here
            });
    }

    public void Configure(IApplicationBuilder app)
    {
        // This inserts the middleware that will execute the 
        // default authentication scheme handler on every request
        app.UseAuthentication();

        app.UseMvc();
    }
}

这样做意味着当 MVC 评估请求是否被授权时,身份验证已经发生,因此不为AuthenticationSchemes属性的[Authorize]属性指定任何值不会有问题。

该过程的授权部分仍将运行并检查经过身份验证的用户是否属于该Administrator组。

于 2017-11-02T05:17:10.727 回答
2

我知道这个问题已经有了答案,但是这里遗漏了一些重要的东西。您需要确保您实际上是为登录用户设置声明。就我而言,我使用的是 JWT 身份验证,所以这一步非常重要:

    var claims = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, user.UserName) });
    var roles = await _userManager.GetRolesAsync(user);
    if (roles.Count > 0)
    {
        foreach (var role in roles) { claims.AddClaim(new Claim(ClaimTypes.Role, role)); }
    }

    var token = new JwtSecurityToken(
        issuer: _configuration["JWT:Issuer"],
        audience: _configuration["JWT:Audience"],
        expires: DateTime.UtcNow.AddMinutes(15),
        signingCredentials: signingCredentials,
        claims: claims.Claims);

我一直在努力想弄清楚为什么HttpContext.User没有包括我所期望的试图缩小[Authroization(Roles="Admin")]问题范围的内容。事实证明,如果您使用的是JWT Auth,您需要记住将 设置Claims[]为身份。也许这是以其他dotnet方式自动完成的,但jwt似乎需要您手动设置。

在我为用户设置声明后,[Authorize(Roles = "Whatever")]按预期工作。

于 2019-09-22T04:09:33.473 回答