How to get JWTToken from ASPNET Core Controller?

Viewed 9187

I'm trying to make a request to a protected API, so I need to add a authorization request header to HttpClient like this:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");

But how to get the Authentication Token ("Your Oauth token") from a controller?

PS: I am already authenticated to Identity Server 4. Application developed in AspNetCore.

Full code:

    [Authorize] //Already authenticated
    public IActionResult SomeControllerAction()
    {
        var claimsIdentity = User.Identity as ClaimsIdentity; //where is JWTToken??
        var JWTTokne = "how to get?";

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", JWTTokne);
            var result = client.PostAsync("someurl", new StringContent(json, Encoding.UTF8, "application/json")).Result;
            //more code to handle result....
        }

        return View();
    }
4 Answers
Related