Retrieve JSON web token payload data in controller action

I am implementing JWT-based authorization for my ASP.NET Web API application using Angular2 SPA. Everything is fine and understandable with respect to the flow of authority, with the exception of one detail. I am wondering how to get JWT payload information in a web API controller action?

Looking through the Internet, I can’t find a solution that I could turn to, for example, by installing Thread.Principalor something like that.

What are the recommended ways of doing this?

+4
source share
1 answer

The usual process for handling a JWT token as authentication in ASP.NET is:

  • Get the token from the request and make sure it is valid.
  • , , , .

, , , . , JWT , ​​ , .

, OWIN , OWIN , .

, , , ASP.NET Web API (OWIN) :

, :

[HttpGet]
[Authorize]
[Route("claims")]
public object Claims()
{
    var claimsIdentity = User.Identity as ClaimsIdentity;

    return claimsIdentity.Claims.Select(c =>
        new
        {
            Type = c.Type,
            Value = c.Value
        });
}

User.Identity, , . , , JWT, API.

+3

Source: https://habr.com/ru/post/1656523/


All Articles