Search code examples
c#permissionsjwtasp.net-core-webapiroles

C# .net core 7.0 Web API permission management, get 404 undocumented when I run the query


This is my Controller-Method i want to call:

[Authorize(Roles = "Manager")]
[HttpGet]
[Route("getTest")]
public async Task<ActionResult> getTest()
{
   return Ok();
}

My role configuration in the program.cs:

builder.Services.AddAuthorization(options =>
{

    options.AddPolicy("Role", policy => policy.RequireRole("Manager"));

});

This is the JWT bearer that I give with the query. If I only write the [Authorize] attribute via the method, the query works, it seems like the problem lies in recognizing the role.

{
  "nameid": "1e12h802-3957-4jde-ae30-52ah3d51b321",
  "email": "mail@mail.com",
  "unique_name": "Max",
  "permissions": [
    "users.edit",
    "users.delete"
  ],
  "role": "Manager",
  "nbf": 1676363429,
  "exp": 1676449829,
  "iat": 1676363429,
  "iss": "http://issuer.com",
  "aud": "http://audience.com"
}

When I call the "getTest" method, I get a 404 undocumented error. The query works without the role specification. Does anyone have an idea what the problem could be?

I tried to change the jwt creating, change the role specification in the controller method and change the program.cs configurations


Solution

  • SOLVED: Adding [Authorize(AuthenticationSchemes = "Bearer")] solved the problem for me.