0

I need to run some logic in an ASP.NET API action using the claims in the passed in access token. How do I get the access token? And later how do I get the claim value in the access token?

[HttpGet]
[Authorize(Policy = "PaidMember")]
[Route("GetVideos")]
public async Task<IEnumerable<VideoModel>> GetVideos(string topicId)
{
    var accessToken = ... // TODO: Get the access token here

    // Then something like the following
    var type = accessToken.GetValue("membershiptype") // This line is made up
    return await _service.GetVideos(topicId, type );
}
M. Azyoksul
  • 1,580
  • 2
  • 16
  • 43
  • 1
    this [link](https://stackoverflow.com/questions/49768774/how-to-get-access-token-from-httpcontext-in-net-core-2-0) can be useful to you – Sina Riani Dec 18 '20 at 09:34

3 Answers3

2

You can get the claims with :

HttpContext.User.Claims

See here for get a specific claim : Extract values from HttpContext.User.Claims

I suggest you to use the ASP pipeline with Authorization middlware if you want to check claims. See Microsoft Docs - ASP.NET Core Middleware.

Emilien Mathieu
  • 301
  • 3
  • 16
1

You can get the claim value like this:

(User.Identity as ClaimsIdentity).FindFirst("membershiptype").Value

With this you do not need to get the original token

0

you can go through the following link as a reference for your queries https://www.youtube.com/watch?v=B2jDN53taDk&list=PL6n9fhu94yhW7yoUOGNOfHurUE6bpOO2b&index=23

firoz khan
  • 27
  • 7