Search code examples
asp.net-mvcasp.net-web-apiasp.net-mvc-5asp.net-identityasp.net-identity-2

Web API Bearer token authentication Authorize doesn't work


I am developing ASP.NET Web API application with Identity 2.0 Bearer authentication. I have configured bearer authentication and implemented SignIn method, so that when I pass header:

Authorization: Bearer uGjAWZA1nPc1AqPuKpAzPhb989SYbtGd...

It works normally. I can call User.Identity.GetUserId() or User.Identity.GetUserName(). I even can check if user is in some role or not, and it also works good.

What is doesn't work, is [Authorize] attribute. When I specify roles, like that [Authorize(Roles = "Admin")] it returns:

{
    Message: "Authorization has been denied for this request."
}

Any thoughts?


Solution

  • Have you added Role claim when creating your identity?

    userIdentity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));