0

I have an URL to generate jwt token for API calls. I want to create a web api action method with token autherizion. How can I validate the token inside the action method?

    [Authorize]
    [HttpPost]
    [Route("api/customer")]
    public String customer(APICustomer APICustomer1)
    {
try
    {
    insert_Customer(APICustomer1.custid,APICustomer1.custname,APICustmer1.status);
    }
    catch (Exception ex)
    {} 
    return APICustomer1.custid+": is Inserted";
    }
George
  • 1
  • 2
  • 1
    Hi, jwt token is already validated by [Authorize] tag. It works if you set all correctly – ZanoOnStack Mar 30 '22 at 11:14
  • How could I get user name from the token? – George Mar 30 '22 at 12:04
  • You should use ClaimsIdentity read this: https://stackoverflow.com/questions/38340078/how-to-decode-jwt-token – ZanoOnStack Mar 30 '22 at 12:46
  • Assuming that you have included a claim for the userName with type "ClaimTypes.Name" you can access it inside your controller from the HttpContext: httpContext.User.Claims.Single(x => x.Type == ClaimTypes.Name).Value; – spyros__ Apr 04 '22 at 14:36

0 Answers0