首页 文章

.Net Core 2 JWT,Angular 2通过角色授权不起作用

提问于
浏览
0

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

{"sub":"flamelsoft@gmail.com","jti":"0bca1034-f3ce-4f72-bd91-65c1a61924c4",“http://schemas.microsoft.com/ws/2008/06/identity/claims/role ": "管理员", " 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}");
        }
    }

user.service.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 });
    }
}}

问题是角色的验证不起作用,请求到达控制器并在 Headers 中返回代码200,但从不进入类 . 当我删除[授权(角色=“管理员”)]时,它正确输入我的代码 . 有没有严格定义的东西?或者通过角色定义授权的替代方案是什么 .

1 回答

  • 2

    TL; DR

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

    [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承载认证时的default authentication scheme name .


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

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

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

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


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

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

    使用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评估请求是否被授权时,已经进行了身份验证,因此不指定 [Authorize] 属性的 AuthenticationSchemes 属性的任何值都不会有问题 .

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

相关问题