# How to Secure Your .NET Web API with Token Authentication

**URL:** <https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821>\
**Category:** Developer Blog Comments\
**Created:** [February 1, 2018, 7:09am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821 "2018-02-01T07:09:51Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [February 22, 2019, 5:49pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/21 "2019-02-22T17:49:16Z")

</div>

**leebrandt**

I will put this on the list and try to get one out very quickly. I haven’t upgraded my box to 2.2 yet. And 3.0 is almost out! 😃

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [February 22, 2019, 6:15pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/22 "2019-02-22T18:15:18Z")

</div>

**leebrandt**

I have fixed the write up to amend the security section to add this. thanks @goobering!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [March 14, 2019, 2:06am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/23 "2019-03-14T02:06:51Z")

</div>

**Narayana katooru**

What is your suggestion on API client itself generating token and sending as part of GET requests? instead of passing secret id and secret answer over the wire? do you see any disadvantage or security risk with this approach? For me it looks like more hassle for client to generate token on its own unless server provides an console to generate token ( Like how crircleCI does it)

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 16, 2019, 2:06pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/24 "2019-04-16T14:06:46Z")

</div>

**leebrandt**

[https://developer.okta.com/…](https://developer.okta.com/blog/2019/03/13/build-rest-api-with-aspnet-web-api "https://developer.okta.com/blog/2019/03/13/build-rest-api-with-aspnet-web-api") This might be what you’re looking for.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 16, 2019, 2:12pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/25 "2019-04-16T14:12:05Z")

</div>

**leebrandt**

I think this depends on how you’re using your API. If you are creating an API for your private consumption (the only client being your client/spa app) then I think just having your API store it’s own client id and secret is fine. You’ll probably only have your client app authenticate users and sending the user’s token and delegating authority to your API.

If you’re creating an API to make it public and be able to control access to everyone’s client apps, then they will likely be sending a client id and secret to get an access token, then just sending the access token with each request.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 16, 2019, 2:23pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/26 "2019-04-16T14:23:13Z")

</div>

**Narayana katooru**

Okay. Thank you

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [May 23, 2019, 3:25pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/27 "2019-05-23T15:25:03Z")

</div>

**lhudson2**

Hi Lee, do you have a working sample application and API that demonstrates this in GitHub? Would be helpful if you did.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [June 18, 2019, 7:01pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/28 "2019-06-18T19:01:56Z")

</div>

**Vijay Ganapathy**

You are awesome! Worked!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [June 18, 2019, 7:49pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/29 "2019-06-18T19:49:23Z")

</div>

**Vijay Ganapathy**

Hey Lee,

Thanks for the article, helped me very much! I see that the TokenService is configured as singleton and if a valid token exists then the GetToken method returns that instead of creating a new one. If I have two consumers, both servers with different clientid and secret, this setup I believe could return one consumer’s token to another. In such cases, what’s the best way to handle this? Should I change the TokenService to be transient? Any other better ways of handling this?

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [June 19, 2019, 1:03am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/30 "2019-06-19T01:03:43Z")

</div>

**Vijay Ganapathy**

Another question, if I have two consumers and I want to provide one consumer access and deny access to other consumer for a specific endpoint, how do I do that? Since just by decorating with the [Authorization] attribute does it all, how do I restrict access?

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [June 25, 2019, 8:10pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/31 "2019-06-25T20:10:03Z")

</div>

**Rosario Martone**

Hi all, just a quick stupid question. I have implemented the API as for the example and I am getting 401 while launching the API from Visual Studio. My issue is on POSTMAN, I am not able to provide the client id and ClientSecret and always getting 401. Any tip? Please! 🙂

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [October 23, 2019, 10:07pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/32 "2019-10-23T22:07:30Z")

</div>

**Luke Konecki**

I can get a token with Postman no problem but with this code I am getting the error ”An existing connection was forcibly closed by the remote host” right away. Has anyone run into this issue before?

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [November 13, 2019, 9:46am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/33 "2019-11-13T09:46:39Z")

</div>

**bigjump**

This didn’t work for me initially either - I was getting an invalid\_scope error.- "The authorization server resource does not have any configured default scopes, ‘scope’ must be provided."

This article helped me figure it out - [https://developer.okta.com/…](https://developer.okta.com/blog/2018/10/16/token-auth-for-java "https://developer.okta.com/blog/2018/10/16/token-auth-for-java")

"The Client Credentials grant type requires us to define a custom scope."

You just need to add a new scope to the Authorization Server and request using that new scope.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 10, 2020, 12:16am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/34 "2020-04-10T00:16:05Z")

</div>

**Castellionee**

After getting the correct Token  
/api/UserBasicInfo?access\_token={THE TOKEN}

I still receive http error 401.  
What Am I missing??  
It has been days and I’m running in circles.  
Your help is really appreciated.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 21, 2020, 4:14am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/35 "2020-04-21T04:14:15Z")

</div>

**Patrik Rose**

Followed all the steps in this tutorial. One issue though  
In GetNewAccessToken() this line:   
`var json = await response.Content.ReadAsStringAsync();`  
returns this HTML and not JSON:  
`<noscript> <div id=“noscript-msg” class=“noscript-msg”> <div class=“noscript-content”> <h2>Javascript is required</h2> <h1>Javascript is disabled on your browser. Please enable Javascript and refresh this page.</h1> Refresh </div> </div></noscript>`

This causes an exception to be thrown.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [June 12, 2020, 1:32am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/36 "2020-06-12T01:32:43Z")

</div>

**dennis602**

Can you show how to call this from Postman? How would you pass in the clientid and secret from postman? Trying to see what is returned using postman.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [June 16, 2020, 1:45pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/37 "2020-06-16T13:45:21Z")

</div>

**Tim H**

As per the instructions, I have my TokenUrl as “[https://dev-xxxxxx.okta.com…](https://dev-xxxxxx.okta.com/oauth2/default/v1/token "https://dev-xxxxxx.okta.com/oauth2/default/v1/token")”, but I am getting a 400: Bad Request returned when requesting a token. Is this URL correct?

Above, it is mentioned that one can find the URLs in the Admin portal: “…going to the dashboard and hovering over the API menu item in the menu bar, then choosing Authorization Servers from the drop down menu”. This yields the authority URL: [https://dev-XXXXXX.okta.com…](https://dev-XXXXXX.okta.com/oauth2/default "https://dev-XXXXXX.okta.com/oauth2/default").

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [July 7, 2020, 6:07pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/38 "2020-07-07T18:07:02Z")

</div>

**Vikas Verma**

Check token API in Postman to find exact error. If the error is “invalid scope” you need to add access\_token as scope in okta application(as mentioned in below comments)   
API =\>Authorization Servers =\> default (your app) =\> Scopes tab. Add access\_token.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [July 7, 2020, 6:15pm UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/39 "2020-07-07T18:15:53Z")

</div>

**Vikas Verma**

Getting 401, Unauthorized. Even after following the post as is and adding app.UseAuthentication()

{StatusCode: 401, ReasonPhrase: ‘Unauthorized’, Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:  
{  
 Transfer-Encoding: chunked  
 Server: Microsoft-IIS/10.0  
 WWW-Authenticate: Bearer  
 X-Powered-By: [ASP.NET](http://ASP.NET)  
 Date: Tue, 07 Jul 2020 16:54:45 GMT  
}}

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 2, 2020, 3:26am UTC](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821/40 "2020-09-02T03:26:01Z")

</div>

**Tommy Wu**

1. Create a new request and change the Request Type to **POST**. (upper left)  
2. Select the **Authorization** tab (below the word POST and to the right of Params)  
3. In the dropdown below TYPE, select **OAuth 2.0**  
4. A orange **Get New Access Token** should show.   
5. Click that Get New Access Token button and fill in the info from your okta org into the form you see below.

[https://uploads.disquscdn.c…](https://uploads.disquscdn.com/images/bc0d57da3ee013c366121587b9d64c040febcb01710e741addc06e0435b5a7f3.png "https://uploads.disquscdn.com/images/bc0d57da3ee013c366121587b9d64c040febcb01710e741addc06e0435b5a7f3.png")

[Previous page](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821.md?page=1)

[Next page](https://devforum.okta.com/t/how-to-secure-your-net-web-api-with-token-authentication/16821.md?page=3)
