User.Claims Is Empty In MVC Application

后端 未结 1 1474
有刺的猬
有刺的猬 2021-02-15 13:19

I\'m working on upgrading my .NET Core 2.2 MVC application to 3.0. In this application I\'m authenticating to a controller using a JWT token. The token contains several claims,

相关标签:
1条回答
  • 2021-02-15 13:34

    It seems the user isn't authenticated at all.

    With asp.net core 3.0 routing has changed to Endpoint routing. You can opt-out by setting EnableEndpointRouting = false.

    But that seems not the case here. That means you'll have to include certain services when you use them, like authentication and authorization:

    public void Configure(IApplicationBuilder app)
    {
      ...
    
      app.UseStaticFiles();
    
      app.UseRouting();
      app.UseCors();
    
      app.UseAuthentication();
      app.UseAuthorization();
    
      app.UseEndpoints(endpoints => {
         endpoints.MapControllers();
      });
    

    And most important, in that order. As documentated here: Migrate Startup.Configure.

    0 讨论(0)
提交回复
热议问题