✅ 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
GitHub
GitHub - BekirK-C/Simpra_Final_Project
Contribute to BekirK-C/Simpra_Final_Project development by creating an account on GitHub.
4 Replies
@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-updStack 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 ...
Adding
System.IdentityModel.Tokens.Jwt
nuget package (version 6.31.0) fixes the issue.
Not really sure the 'why' of it, though.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
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.