0
votes

So a while back I was able to create login, register, edit user and get all users functions. These worked fine as I was able to register a user. Login them in and then also edit those credentials. Of course some JWT tokens were used for authentication. I used an API call on Postman to edit current users: PUT Request

This worked fine until recently where even if I add the Bearer token unto the Authorization header, I still get a "401 Unauthorized" error. This also happens when I use Insomnia to try the same request. I am using Angular as the front-end and its service(s) is still working fine as I am able to both login and register users. Whilst in postman/insomnia I am only able to login and get the bearer token. All other POST/GET requests linked to the /users/ no longer work.

Other GET requests work fine and return a 200 response.

This is the output from a PUT request:

PUT /users/11 HTTP/1.1 

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjkiLCJuYmYiOjE1OTA2NTUxMjcsImV4cCI6MTU5MTI1OTkyNywiaWF0IjoxNTkwNjU1MTI3fQ.eYd4oUpE7i4PXByt2aUI-4OGCl0N_vGCmaBgib28uTQ

Content-Type: application/json

User-Agent: PostmanRuntime/7.25.0

Accept: /

Cache-Control: no-cache

Postman-Token: cea6f9a6-46bb-41b1-a711-7fc1f8886858

Host: localhost:5000

Accept-Encoding: gzip, deflate, br

Connection: keep-alive

Content-Length: 29

{ "Password": "Eskimo_321" }

HTTP/1.1 401 Unauthorized

Transfer-Encoding: chunked

Server: Microsoft-IIS/10.0

WWW-Authenticate: Bearer

X-Powered-By: ASP.NET

Date: Thu, 28 May 2020 09:38:10 GMT

1
might be JWT token you are using in postman is expired, did you tried with a new token in postman?Vipul Patil
@VipulPatil yes I have done that numerous times. I even added swashbuckle to my asp.net core project to see if that would help. The users/registration POST request now works on swagger but not on Postman the other still the same. Only when I add: [AllowAnonymous] [HttpPut("{id}")] does the put work but then that defeats the whole purpose of authorization.Wandu

1 Answers

0
votes

Found the issue, had rebuilt my project and had forgotten to add:

app.UseAuthentication();

on the Startup.cs file. All the routes needed authorization except the Login and register routes.