C
C#13mo 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
TravestyOfCode13mo ago
@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
TravestyOfCode13mo ago
Adding System.IdentityModel.Tokens.Jwt nuget package (version 6.31.0) fixes the issue. Not really sure the 'why' of it, though.
BekirK
BekirK13mo 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
Accord13mo 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
More Posts
✅ Implementing IDisposable properlyI have a class I implemented IDisposable in which subscribes to an observable of a type. Example: su❔ Help with Assemblies and the TypeRef tableI am trying to load a .NET DLL as an assembly and read the TypeRef table. End goal is to get all the❔ Python Decorators in C# ??? How do i intercept method calls with attributes?Is is possible to intercept/prevent a method call in c# with attributes? i was thinking about someth❔ AspNetCore web MVC app - AuthenticationI would like to implement some authentication to my web app. I would like to make it "proper" and no❔ .net 7 docker container error: qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2' HELPI am getting this error (qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or ✅ Cant open .csporjSo i made a WFA and i want to edit the .csproj file but i cant open it✅ Restore a PackageReference and use contained MSBBuild scripts inside of .targets fileHey folks, I have a CICD script that executes a custom target `Foo.targets` using `dotnet msbuild F❔ Cross-platform 3D rendering library?Hello, I am working on an application/tool to generate 3D models. I was wondering if there was a goo✅ How to make a custom converter in .net 6.0 using wpf?Hello, sorry I couldn't find anything useful using google so i'm asking here, sorry if it's obvious.❔ How does the main method work?I've only ever used c# in unity, so i've never worked outside the context of making games, so using