ASP.NET Core 3 API忽略Bearertoken的授权属性

时间:2019-04-20 17:13:28

标签: c# asp.net .net asp.net-web-api asp.net-core

我正在研究ASP.NET Core Web API。我正在使用最新的版本3.0.0-preview4.19216.2。

我有一个问题,我的API控制器忽略了Authorize-Attribute,但是在另一个控制器上,该属性可以正常工作。

    [Route("api/[controller]")]
    [ApiController]
    [Authorize(AuthenticationSchemes =JwtBearerDefaults.AuthenticationScheme)]
    public class SuppliersController : ControllerBase
    {

        [HttpGet("GetAll")]
        public IActionResult GetAll()
        {
            var companyId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == "Company_Id").Value); // throws nullreference exception

            return Ok();
        }
    }

但是在另一个控制器上我有类似的东西,但是该属性按预期工作

    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UsersController : ControllerBase
    {
        [HttpGet("{id}")]
        public IActionResult GetById(int id)
        {
            var test = User.Claims.FirstOrDefault(c => c.Type == "Company_Id").Value;
        }

    }

在用户控制器中,一切正常。

我也在没有

的SupplierController中尝试了它
  

AuthenticationSchemes

但没什么。

这是我在Startup.cs中的AddAuthentication

services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.Events = new JwtBearerEvents
                {
                    OnTokenValidated = context =>
                    {
                        var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
                        var userId = int.Parse(context.Principal.Identity.Name);
                        var user = userService.GetById(userId);
                        if (user == null)
                        {
                            // return unauthorized if user no longer exists
                            context.Fail("Unauthorized");
                        }
                        return Task.CompletedTask;
                    },
                    OnAuthenticationFailed = context =>
                    {
                        Console.WriteLine(context);

                        return Task.CompletedTask;
                    },
                    OnMessageReceived = context =>
                    {
                        return Task.CompletedTask;
                    }
                };
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });

这是我完整的startup.cs

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            var appSettingsSection = Configuration.GetSection("AppSettings");
            services.Configure<AppSettings>(appSettingsSection);

            AuthenticationService.ConfigureSchoolProjectAuthentication(services, appSettingsSection);
            DependencyInjectionService.Inject(services);

            services.AddMvcCore()
                .AddNewtonsoftJson();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

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

            app.UseRouting();
            app.UseEndpoints(routes =>
            {
                routes.MapControllers();
            });
        }
    }

奇怪的是,当我的SupplierController调用时,我的授权逻辑没有被调用(已通过调试器检查),而当我的UserController调用时,该逻辑被执行了。

我认为这是索赔无效的原因。但是,当控制器具有授权属性时,为什么不调用逻辑?

我的身份验证似乎无法正常工作,因为我可以通过在Postman中不使用身份验证来访问所有控制器。我在做错她的事吗?

1 个答案:

答案 0 :(得分:1)

好吧,我在这篇博客文章ASP.NET Core updates in .NET Core 3.0 Preview 4

中找到了答案。

我必须从

更改身份验证注册的顺序
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

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

        app.UseRouting();
        app.UseEndpoints(routes =>
        {
            routes.MapControllers();
        });
    }

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        app.UseRouting();

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

        app.UseEndpoints(routes =>
        {
            routes.MapControllers();
        });
    }

所以这解决了我的问题。