0

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!

2
  • Does this answer your question? Web api core returns 404 when adding Authorize attribute Commented Feb 17, 2021 at 16:06
  • thanks but no , i edit my question when I change to [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] I got 401 Unauthorized Commented Feb 18, 2021 at 9:06

2 Answers 2

0

When you add [Authorize], this action can only be successfully accessed if the token is carried. One of possibilities is that the url is not mathced with this route. But a more important reason is that you do not configre the login path correctlly, or the login path does not exist. So you could check the configuration in startup.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your response , i have API that work in postMan WITHOUT [Authorize] so the URL is correct and I test it with POSTMAN i just want to add object in database , when I write [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] I got 401 Unauthorized even when I added token in header :/
i edit my question and i added my startup
How do you send the token when using Jwt Authentication Scheme?
i have api post for Authentication that accept email and password and return the token I added now the code in the question
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.