I am attempting to modify and use the downloaded sample ASP.NET Core MVC app that has been configured with my tenant and - seemingly - my API information to access my ASP.NET Core Web API. I can successfully login on the MVC app and I can verify that I have both an id_token and an access_token. When the authenticated user clicks a link I added to the Home Controller, control passes to the Values method in my controller. Within that method I am retrieving the access_token like this:
[Authorize]
public async Task<IActionResult> Values()
{
string accessToken = await HttpContext.GetTokenAsync("access_token");
var client = new RestClient("http://localhost:3010/api/private");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", $"Bearer {accessToken}");
IRestResponse response = client.Execute(request);
return View();
}
I know that the user is authenticated because I have the Authorize attribute on the Values method. Within the method you can see that I am obtaining the access_token and attempting to pass it as a bearer token to my API. However, no matter what I try, I am getting a 401 Unauthorized response.
I have verified the body of the access_token. Here’s what that looks like:
{
"iss": "https://centurysoftwaretech.auth0.com/",
"sub": "auth0|5d375597fd9aa60eff16fe3d",
"aud": [
"http://localhost/auth0testapi",
"https://centurysoftwaretech.auth0.com/userinfo"
],
"iat": 1563918460,
"exp": 1564004860,
"azp": "THE_CLIENTID_OF_MY_APPLICATION_ NOT_MY_API",
"scope": "openid read:messages"
}
I have replaced the azp claim value above with an observation. As far as I can tell, the only bit of information that has anything to do with my API is the “http://localhost/auth0testapi” aud claim… What can I be missing?
The title of my topic states that I am confused by the API Test tab C# code. I am because on that tab it shows that I should make the following call and then retrieve the access_ token from that. But my user is already authenticated! Do I need to do this again?
var client = new RestClient("https://centurysoftwaretech.auth0.com/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"client_id\":\"THE_CLIENTID_OF_MY_API\",\"client_secret\":\"MY_CLIENT_SECRET\",\"audience\":\"http://localhost/auth0testapi\",\"grant_type\":\"client_credentials\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
So in my Values method do I have to call the code above, then extract the access_token, and then call my API? I am confused.