C
C#4mo ago
Aye

swagger Error: response status is 401

Hi. I've a c# web project; it's about a phonebook with two entities: user and contact. I use Swagger to prove the endpoints and it's works with the user's ones but throw an error in the case of contacts's endpoints. The project has jwtSecurityToken, it's generates correctly but i haven't get the contact's endpoints works. The console only indicates: "Failed to load resource: the server responded with a status of 401". If someone can help me, I could send parts of the code and review the error. And this is the remote repo https://github.com/AyeVillagra/AgendaTelefonicaApi Thanks!
GitHub
GitHub - AyeVillagra/AgendaTelefonicaApi
Contribute to AyeVillagra/AgendaTelefonicaApi development by creating an account on GitHub.
10 Replies
Pobiega
Pobiega4mo ago
401 is unauthorized - something about your auth stuff isnt working I've yet to check the actual code you posted, but have you verified that the token contains the claims you expect?
Aye
Aye4mo ago
I not understand so much about the verified claims (and c# general), but i've also check the code with of my colegues and don't find differences about it.
Pobiega
Pobiega4mo ago
looking at your code, I dont see any auth policies being declared? okay, so the only thing your code checks is that a JWT is passed and that it passes validation ignore the claims then, but this probably means the token you are issuing isnt passing validation? manually verify a token
Aye
Aye4mo ago
how I can verify manually the token?? and why it works for the user's endpoints? any idea?
Pobiega
Pobiega4mo ago
because they are unauthorized 😄 they allow any and all calls
[Route("api/[controller]")]
[ApiController]
//[Authorize]
public class UserController : Controller
[Route("api/[controller]")]
[ApiController]
//[Authorize]
public class UserController : Controller
vs
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class ContactController : ControllerBase
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class ContactController : ControllerBase
Aye
Aye4mo ago
🤦 okey, i'll try changing that.. thanks!
Pobiega
Pobiega4mo ago
I mean, it will just give you 401s on the usercontroller too your token creation looks fine at a glance, but I must admit JWT authentication isnt my area of expertise and you are SURE you are passing the token to the server, right? 😄
Aye
Aye4mo ago
i think so... the authentication pass in the swagger and i can see it in the request.. but I don't have enough knowledge to see if everything is ok. I feel like I'm going round in circles about the same thing and perhaps the error is in another part 😵‍💫
Pobiega
Pobiega4mo ago
yeah debugging stuff thats going on in the middleware isnt trivial
Aye
Aye4mo ago
Hi! using Postman instead of Swagger, the endpoints works with the Token. Mystery.