# Call API from ASP.Net Core 2.0

**URL:** <https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591>\
**Category:** Questions\
**Tags:** dotnet\
**Created:** [April 11, 2018, 5:33pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591 "2018-04-11T17:33:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bleonard](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bleonard/32/435_2.png) [@bleonard](https://devforum.okta.com/u/bleonard)\
**Post date:** [April 11, 2018, 5:33pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/1 "2018-04-11T17:33:39Z")

</div>

Please forgive me for the novelty of this question or if it has been covered somewhere else; I’ve been looking all over for a working example but haven’t found anything. I’m a novice in both C#/ASP.NET Core 2.0 and the Okta platform.

I was able to set up a basic working solution where the user authenticates using the [OpenID quickstart](https://developer.okta.com/quickstart/#/okta-sign-in-page/dotnet/aspnetcore).

Now I’m trying to call the Okta API from the backend to get information about “me” as a logged in user. Eventually, I want to get groups and role information (i.e., is the visitor a super user or admin, etc.) but am trying to start simple.

Here is my controller

```auto
[Authorize]
public IActionResult ShowUser()
{
  using (HttpClient client = new HttpClient())
  {
    client.BaseAddress = new Uri("https://{{My Company}}.oktapreview.com");

    byte[] cred = UTF8Encoding.UTF8.GetBytes("{{My API Token}}");

    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
        "Bearer", Convert.ToBase64String(cred));

    HttpResponseMessage response = client.GetAsync("/api/v1/users/me").Result;
    string stringData = response.Content.ReadAsStringAsync().Result;

    // Just for demo purposes
    ViewData["Results"] = stringData;

    return View();
  }
}

```

The error I’m receiving is:

```auto
{
"errorCode":"E0000005",
"errorSummary":"Invalid session",
"errorLink":"E0000005",
"errorId":"oaeOrQDpIWDTRyihIThHokg4g",
"errorCauses":[]
}

```

Can anyone point me in the direction of what I’m doing wrong here?

---

<div class="post-metadata">

**Author:** ![vijet](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/vijet/32/245_2.png) [@vijet](https://devforum.okta.com/u/vijet)\
**Post date:** [April 11, 2018, 6:14pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/2 "2018-04-11T18:14:37Z")

</div>

If you look at the “get current session” API - [Sessions | Okta Developer](https://developer.okta.com/docs/api/resources/sessions#get-current-session), you will notice the following -

> This operation requires a session cookie for the user. API token is not allowed for this operation.

Make sure that your session cookie is set (user is logged in) and don’t pass the API Token in your code.

Here’s an older thread which might be helpful - [OKTA Session Id Retrieval - Stack Overflow](https://stackoverflow.com/questions/38722455/okta-session-id-retrieval)

---

<div class="post-metadata">

**Author:** ![nate.barbettini](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/nate.barbettini/32/385_2.png) [@nate.barbettini](https://devforum.okta.com/u/nate.barbettini)\
**Post date:** [April 11, 2018, 7:59pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/3 "2018-04-11T19:59:48Z")

</div>

Hey @bleonard! It sounds like you’ve been able to log a user in via OpenID Connect, but you want to get more info about them. At a high level, you have two options:

- Look at the claims in the `ClaimsIdentity`. The claims in theID token returned from Okta (which is consumed by the [ASP.NET](http://ASP.NET) Core OpenID Connect middleware) are automatically put into the `ClaimsIdentity` that represents the user. For example, you can do

```auto
var name = HttpContext.User.Claims.FirstOrDefault(x => x.Type == "name")

```

- If you want more than the few claims that come through the ID token, you can use the [Okta .NET SDK](https://github.com/okta/okta-sdk-dotnet) to easily call the Okta Users API. The user’s Okta ID is available via the `sub` claim. For example:

```auto
var user = await client.User.GetUserAsync("<user id>");

```

We recently updated the [Okta + ASP.NET Core 2.0 example](https://github.com/oktadeveloper/okta-aspnetcore-mvc-example/blob/master/OktaAspNetCoreMvc/Controllers/AccountController.cs#L51-L65) to demonstrate this, in case you want another example. 🙂

Let me know if this is what you’re looking for!

---

<div class="post-metadata">

**Author:** ![bleonard](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bleonard/32/435_2.png) [@bleonard](https://devforum.okta.com/u/bleonard)\
**Post date:** [April 12, 2018, 3:30pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/4 "2018-04-12T15:30:13Z")

</div>

@nate.barbettini This looks very promising, thanks for the update!

I do have one (probably dumb) question regarding the OrgUrl in appsettings.json. In the [QuickStart](https://developer.okta.com/quickstart/#/okta-sign-in-page/dotnet/aspnetcore) the Org Url is said to be found “On the home screen of the developer dashboard, in the upper right.” I don’t see anything that looks like an Org Url when I look at https://{{MyCompany}}-admin.oktapreview.com/admin/dashboard. Am I looking in the wrong spot?

---

<div class="post-metadata">

**Author:** ![tom](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/tom/32/24_2.png) [@tom](https://devforum.okta.com/u/tom)\
**Post date:** [April 12, 2018, 10:09pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/5 "2018-04-12T22:09:31Z")

</div>

![wooo-_Developer_Console](https://global.discourse-cdn.com/oktadev/original/1X/ba7608cb885a3ddd836902d5bc7c5e072638299b.png)

You might be in the admin console, you can switch over to the developer console using the picker in the top left .

Your Org URL should be the admin link without the `-admin`

if you are looking at the admin dashboard and your url is:

`my-company-admin.oktapreview.com`

your org url should be:

`my-company.oktapreview.com`

hope this helps!

---

<div class="post-metadata">

**Author:** ![bleonard](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bleonard/32/435_2.png) [@bleonard](https://devforum.okta.com/u/bleonard)\
**Post date:** [April 24, 2018, 2:27pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/6 "2018-04-24T14:27:26Z")

</div>

I was FINALLY able to take another look at this and I’ve run into an interesting problem.

I’ve pulled an updated copy of the [ASP.Net Core MVC example](https://github.com/oktadeveloper/okta-aspnetcore-mvc-example) and have updated appsettings with my credentials.

Nothing fancy but unfortunately I’m receiving a 401 Unauthorized error.

```auto
An unhandled exception occurred while processing the request.
HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()

IOException: IDX10804: Unable to retrieve document from: 'https://{{my-company}}.oktapreview.com/oauth2/default/.well-known/openid-configuration'.
Microsoft.IdentityModel.Protocols.HttpDocumentRetriever+<GetDocumentAsync>d__8.MoveNext()

InvalidOperationException: IDX10803: Unable to obtain configuration from: 'https://{{my-company}}.oktapreview.com/oauth2/default/.well-known/openid-configuration'.
Microsoft.IdentityModel.Protocols.ConfigurationManager+<GetConfigurationAsync>d__24.MoveNext()

```

I’ve verified that the App in Okta exists and that the API Token is good. I’m kind of at a loss since I haven’t done anything outside of adding the respective values. Thoughts?

---

<div class="post-metadata">

**Author:** ![nate.barbettini](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/nate.barbettini/32/385_2.png) [@nate.barbettini](https://devforum.okta.com/u/nate.barbettini)\
**Post date:** [April 24, 2018, 3:53pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/7 "2018-04-24T15:53:25Z")

</div>

From the `IDX10408` error, it appears that aspnetcore wasn’t able to communicate with your Okta domain. What happens when you try to open `https://{{my-company}}.oktapreview.com/oauth2/default/.well-known/openid-configuration` in a web browser?

You should see a JSON document like: [https://nate-example.oktapreview.com/oauth2/default/.well-known/openid-configuration](https://nate-example.oktapreview.com/oauth2/default/.well-known/openid-configuration)

If that’s not working, make sure `{{my-company}}` is correct. As Tom mentioned above, a common problem is including `-admin` in the URL (that is only used to access the web console).

---

<div class="post-metadata">

**Author:** ![bleonard](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bleonard/32/435_2.png) [@bleonard](https://devforum.okta.com/u/bleonard)\
**Post date:** [April 24, 2018, 4:09pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/8 "2018-04-24T16:09:57Z")

</div>

Hi Nate - thanks for yours and Tom’s help.  
When I check the URL in a browser I get

```auto
{
"errorCode": "E0000015",
"errorSummary": "You do not have permission to access the feature you are requesting",
"errorLink": "E0000015",
"errorId": "oaejhmcqeYATAykReV6Nz_yAg",
"errorCauses": []
}

```

So it looks like this feature is denied or turned off? How can I go about activating/enabling it?

The {{-admin}} thing sounds like it trips a lot of people up. I haven’t been including it in my string values as you and Tom have both reminded me not to.

---

<div class="post-metadata">

**Author:** ![ehs\_ziluckm](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@ehs\_ziluckm](https://devforum.okta.com/u/ehs_ziluckm)\
**Post date:** [May 15, 2018, 2:54pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/9 "2018-05-15T14:54:08Z")

</div>

@nate.barbettini, I am having this same issue if you know what is causing it.

---

<div class="post-metadata">

**Author:** ![bleonard](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/bleonard/32/435_2.png) [@bleonard](https://devforum.okta.com/u/bleonard)\
**Post date:** [May 15, 2018, 6:02pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/10 "2018-05-15T18:02:29Z")

</div>

@ehs_ziluckm The free developer tier of Okta includes the API Access Management product. In my case, my company’s implementation of Okta did not include access to API Access Management which resulted in the errors I was seeing and that prompted this original post.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [January 12, 2024, 11:06pm UTC](https://devforum.okta.com/t/call-api-from-asp-net-core-2-0/1591/11 "2024-01-12T23:06:44Z")

</div>

Closed on January 12, 2024, 11:06pm UTC


