Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Web API, JWT and Swagger - 401 is showing as Undocumented instead of Unauthorized

I have an ASP.NET Core Web API 3 app that implements a REST API and uses a JWT bearer token for authorization, and Swagger (Swashbuckle).

My controller has the [Authorize] filter on it, like:

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class MyController : ControllerBase
{
}

Swagger works with my API, and I can generate a JWT token and give to Swagger and it all works well.

But if I try to use Swagger to hit one of my REST endpoints without a JWT token or invalid JWT token, the Swagger UI is showing an error 401 Undocumented, but all the examples I see out on the web show that I should be getting 401 Unauthorized.

(When I hit the same URL with Postman, it does show 401 Unauthorized.)

Before I start ripping out things, any ideas why I might be getting Undocumented instead of Unauthorized?

This is what I see:

enter image description here

When I add the attribute suggested below

 (ProducesResponseType(typeof(ProblemDetails), (int)HttpStatusCode.Unauthorized)])

I see this:

enter image description here

like image 518
JoeD Avatar asked Apr 10 '20 23:04

JoeD


1 Answers

You can add app.UseStatusCodePages() in the Startup.cs.

This will then return a response body response body

like image 153
Gareth Hodgson Avatar answered Oct 16 '22 23:10

Gareth Hodgson