Search code examples
asp.net-coreasp.net-web-apijwttokenauthorize-attribute

ASP.NET Core JWT Authentication to secure webAPI [Authorize] attribute Error 401 Unauthorized


I develop API POST and it works correctly, but when I add [Authorize] attribute I got 401 Unauthorized in the postman even when I add token correct in header

 [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
           
            [HttpPost]
            
            public async Task<ActionResult> Post([FromBody] CViewModel cVM)
            {
    // to do
            }

I just want to secure my API with JWT TOKEN, API authentication work correctly and generate a token but I can't figure why when I added [Authorize] attribute I got Error 401 Unauthorized in my POST API.

THANKS!


Solution

  • the problem is I used different secret key in the startup and generate token method