C
C#2y ago
BekirK

✅ Authorize() Attribute is not working (SOLVED)

Hello everyone, I implemented Jwt in .net 6 for a project. I have shared my JwtHelper and program class. When I add [Authorize()] attribute for getall request in Controller, it doesn't work, I keep getting unauthorize error. I login and accessToken is created then I use it for getall method in Postman like “Bearer xxxxxxxx” but again it returns unauthorized. I've been trying for days and couldn't find the error. Can you help me please You can also access the repo: https://github.com/BekirK-C/Simpra_Final_Project
[HttpGet("getall")]
[Authorize()]
public IActionResult GetAll()
{
var result = _productManager.GetAll();
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
[HttpGet("getall")]
[Authorize()]
public IActionResult GetAll()
{
var result = _productManager.GetAll();
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
GitHub
GitHub - BekirK-C/Simpra_Final_Project
Contribute to BekirK-C/Simpra_Final_Project development by creating an account on GitHub.
4 Replies
TravestyOfCode
@Be-Cr I added "Microsoft.AspNetCore.Authentication": "Debug", and "Microsoft.AspNetCore.Authorization": "Debug" to the Logging section of appsettings When calling the getall action on Products I got an error Method not found: 'Boolean Microsoft.IdentityModel.Tokens.TokenUtilities.IsRecoverableConfiguration(Microsoft.IdentityModel.Tokens.TokenValidationParameters, Microsoft.IdentityModel.Tokens.BaseConfiguration, Microsoft.IdentityModel.Tokens.BaseConfiguration ByRef) That lead me to this stack overflow post: https://stackoverflow.com/questions/70579279/unauthorized-invalid-token-when-authenticating-with-jwt-bearer-token-after-upd
Stack Overflow
Unauthorized (Invalid Token) when authenticating with JWT Bearer To...
After updating the package Microsoft.AspNetCore.Authentication.JwtBearer from version 3.1.14 to 6.0.1, requests with authentication fail with 401 Unauthorized "invalid token". What needs ...
TravestyOfCode
Adding System.IdentityModel.Tokens.Jwt nuget package (version 6.31.0) fixes the issue. Not really sure the 'why' of it, though.
BekirK
BekirKOP2y ago
I am thankful to you. I was constantly checking the codes I wrote to see if there was an error. Even though I proceeded by debugging, I could not get any results. I did not think to add a logging section. It's a pity that there is a library missing but it doesn't give an error for it in the output. However, it is very gratifying to have the error resolved. Thank you very much for your help @TofCode
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server