# Securing Blazor WebAssembly Apps

**URL:** <https://community.auth0.com/t/securing-blazor-webassembly-apps/46661>\
**Category:** Get Help\
**Tags:** dotnet, webassembly, .net, blazor\
**Created:** [July 21, 2020, 3:10pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661 "2020-07-21T15:10:03Z")\
**Posts on this page:** 20\
**Page:** 7

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 21, 2022, 11:51am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/133 "2022-02-21T11:51:40Z")

</div>

Thanks for sharing that @Hawxy !

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [April 1, 2022, 7:30am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/135 "2022-04-01T07:30:37Z")

</div>

Hey folks 👋 , I updated this article to .NET 6.0, and now most of Blazor WASM’s well-known issues are gone, especially the annoying workaround to define a global audience to call an API! 🎉  
Take a look and enjoy!

---

<div class="post-metadata">

**Author:** ![seandrexler](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/seandrexler/32/36434_2.png) [@seandrexler](https://community.auth0.com/u/seandrexler)\
**Post date:** [April 6, 2022, 4:17pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/136 "2022-04-06T16:17:43Z")

</div>

I had trouble just running the dotnet5 branch in the Github repo. Is there a change at Auth0 that would cause it to 401 calling the server from the client?

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [April 6, 2022, 4:58pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/137 "2022-04-06T16:58:52Z")

</div>

Hi @seandrexler,  
Welcome to the Auth0 Community! 👋

This article and the related sample project have recently been updated to work with .NET 6, which solves a few annoying issues with the previous versions.

Regarding your issue, please, can you verify if the associated error message is “invalid token” as shown [here](https://cdn.auth0.com/blog/securing-blazor-webassembly/blazor-app-dev-tools-invalid-token.png)?

If this is the case, you need to set a [default audience](https://auth0.com/docs/get-started/tenant-settings#api-authorization-settings) on your Auth0 tenant.  
The `Microsoft.AspNetCore.Components.WebAssembly.Authentication` package coming with .NET 5 doesn’t support the `audience` parameter.

Take a look also at [this thread](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/55) and [this one](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/17).

Otherwise, please, provide a detailed log of the HTTP requests. See [here for more details on how to capture HTTP requests](https://auth0.com/docs/troubleshoot/generate-and-analyze-har-files)

---

<div class="post-metadata">

**Author:** ![seandrexler](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/seandrexler/32/36434_2.png) [@seandrexler](https://community.auth0.com/u/seandrexler)\
**Post date:** [April 7, 2022, 12:06am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/138 "2022-04-07T00:06:41Z")

</div>

@andrea.chiarelli ,  
Thanks! looks like I missed setting the default audience for my tenant. Once I set that in the tenant settings it worked just fine.  
Since the audience is provided by the client in the dotnet 6 example do you still need the tenant default using dotnet6?

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [April 7, 2022, 7:23am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/139 "2022-04-07T07:23:27Z")

</div>

> Thanks! looks like I missed setting the default audience for my tenant. Once I set that in the tenant settings it worked just fine.

Awesome! Thank you for sharing!

> Since the audience is provided by the client in the dotnet 6 example do you still need the tenant default using dotnet6?

No. you don’t need a default audience with .NET 6 anymore

---

<div class="post-metadata">

**Author:** ![wynnytsky](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/wynnytsky/32/37384_2.png) [@wynnytsky](https://community.auth0.com/u/wynnytsky)\
**Post date:** [May 17, 2022, 4:48pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/140 "2022-05-17T16:48:22Z")

</div>

The logout was working intermittently for me in Chrome (in Brave it always worked). I compared it with the Okta sample and I noticed that the logout link was a button instead of an anchor. Sure enough, changing this …  
`<a href="#" @onclick="BeginSignOut">Log out</a>`

… to this …  
`<button class="nav-link btn btn-link" @onclick="BeginSignOut">Log out</button>`

resolved it

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 18, 2022, 2:23pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/141 "2022-05-18T14:23:59Z")

</div>

Thanks for sharing it with the rest of community!

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [May 18, 2022, 2:51pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/142 "2022-05-18T14:51:35Z")

</div>

Interesting. I will try!  
Anyway, this makes me think that it’s a browser issue 🤔

---

<div class="post-metadata">

**Author:** ![brian.levin](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brian.levin/32/37786_2.png) [@brian.levin](https://community.auth0.com/u/brian.levin)\
**Post date:** [June 2, 2022, 5:07pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/143 "2022-06-02T17:07:21Z")

</div>

Can anyone please explain why there are FOUR calls made to the Auth0 endpoints every time the application starts? I’m using a Dev tenant, and I realize while that is slower than a production one, it is still taking over 2 seconds before the application loads just to get the authentication/authorization. See image below.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/8/9/89db4729856e0d74ccf92b4d76b0f556be69d722.png)

I’ve followed the guide exactly. Am I missing something? Are there settings I can use to maybe reduce this down to 1 or 2 calls? This makes my WASM application too slow for our customer use based on our standards of load times. I can’t use pre-rendering as it doesn’t work with authorization … any suggestions / guidance would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![robertino.calcaterra](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/robertino.calcaterra/32/66825_2.png) [@robertino.calcaterra](https://community.auth0.com/u/robertino.calcaterra)\
**Post date:** [June 2, 2022, 5:35pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/144 "2022-06-02T17:35:14Z")

</div>

Hey @brian.levin as soon as posible, @andrea.chiarelli will help you with this

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [June 3, 2022, 12:27pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/145 "2022-06-03T12:27:44Z")

</div>

Hey @brian.levin,  
Welcome to the Auth0 Community 👋  
The four calls you highlighted are the typical calls of the [Authorization Code Flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow) as per [OpenID Connect standard specifications](https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowSteps). So, I’m afraid you can’t drop them.  
Actually, the only call you could remove is the one to the `userinfo` endpoint. You already have the user’s data in the ID token, so you don’t need to call that endpoint. However, this call is automatically made by the Blazor’s `AuthenticationService.js` script, so you can’t prevent it as far as I know (unless you want to put your hands in that code… 😬)

Anyway, I made some tests with my developer tenant and I get lower times than yours for the call to the `authorize` endpoint, which is the slowest time in your case:

 ![Screen Shot 2022-06-03 at 12.13.33 PM](https://global.discourse-cdn.com/auth0/original/3X/c/d/cd06ed4c56caeb19a9d41865eb658ac7c741a3be.png)

I wonder if it wasn’t a momentary slowdown 🤔

---

<div class="post-metadata">

**Author:** ![van.vo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/van.vo/32/34144_2.png) [@van.vo](https://community.auth0.com/u/van.vo)\
**Post date:** [June 24, 2022, 10:34pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/146 "2022-06-24T22:34:54Z")

</div>

Hi,

I also happened to have a 401 error if anyone can help to point out what did I do wrong in my case.

First of all, I follow this article [How to Secure Blazor WASM Applications with Auth0](https://auth0.com/blog/securing-blazor-webassembly-apps/)  
It is about creating **Blazor WASM** , **Blazor Server** and **authenticate** between them.  
It’s work great no problem.

I based on that article and instead of using **Blazor Server** , I’m creating an **AWS Serverless Project** instead and apply the same setup just like **Blazor Server** to that project.

**------------ SERVER SIDE ------------**  
This **AWS Serverless Project** only have 1 simple controller which contain 2 Get method.  
One of them will have **Authorize** tag

**ValuesController.cs**  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/b/4/b49106cce3f84ea1741c700de52feca2b09f0dca.jpeg)

**Startup.cs**  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/3/8/384790dc414899f3d7f02ce307a21302ebeb04c7.png)  
**Startup.cs** under **Configuration** method  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/6/5/65ce6e44ee0e3f73e9ad8925d86b5e3c2cc76f4a.png)

And of course the **appsettings.json** will contain the **Domain** and **Audience** information just like the article show  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/8/a/8a178bea6e7c7648fed11fe02eaf68849f4c390f.png)

That is all for the server side.  
I then deploy this project to **AWS CloudFormation**.

**------------ CLIENT SIDE ------------**  
I have everything setup just like the article for **Blazor WASM**.  
Only different here is that I will try to get the data and display in the **index page**.

**index.razor**  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/a/f/af5f92aaed485df5bad6ab2483fc7a15ee2af888.jpeg)

**Program.cs**

 ![image](https://global.discourse-cdn.com/auth0/original/3X/e/2/e2b3afc5c14fcf26fe1fd4474dc25fc3aaa1e1d2.png)

And the **appsettings.json** will have **Authority** , **ClientId** and **Audience** information

**------------ AUTH0 DASHBOARD ------------**  
Everything is following the article to **Create new Application** for **Blazor WASM** and **Create New API** for **AWS Serverless**.

**------------ RESULTS ------------**  
When I try to call the second Get method (without the Authorize tag) it will work fine but the other one (with the Authorize tag) even after I **login successfully** it will still **return 401** from the browser console and failed to get the data.

Sorry for the long post but that is all for my case.  
Please help to advice what should I do next to solve this issue.  
Thanks.

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [June 27, 2022, 9:23am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/147 "2022-06-27T09:23:00Z")

</div>

Hi @van.vo,  
Welcome to the Auth0 Community 👋 and thank you for reading the article and trying the sample project.

At first look, I can’t see anything wrong with your project. Maybe you can get more information on the reason for that `401` by capturing the full HTTP response, using Chrome’s Developer Tools, for example.

The first possible reason for this error that comes to my mind is a misspelled audience in the server or client configuration. Please, make sure that the audience’s value you store in the `appsettings.json` files matches the one you registered in the Auth0 dashboard in any detail, including any trailing slash

---

<div class="post-metadata">

**Author:** ![Hawxy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/hawxy/32/21395_2.png) [@Hawxy](https://community.auth0.com/u/Hawxy)\
**Post date:** [August 26, 2022, 2:11pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/148 "2022-08-26T14:11:39Z")

</div>

A small update for the .NET 7 space, this piece of work went in and should be in the next release: [[Blazor][Wasm] Dynamic and extensible authentication requests · Issue #42580 · dotnet/aspnetcore · GitHub](https://github.com/dotnet/aspnetcore/issues/42580)  
This unblocks handling of the Auth0 Organization Invite flow in Blazor, and probably resolves some sore spots elsewhere.

Nothing to report on getting the metadata seed setting passed down as a library option. Due the underlying oidc-client library being end-of-life, I have a feeling this is going to take priority for .NET 8: [Consider re-designing the Blazor WASM authentication stack · Issue #40764 · dotnet/aspnetcore · GitHub](https://github.com/dotnet/aspnetcore/issues/40764)

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [August 29, 2022, 7:49am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/149 "2022-08-29T07:49:04Z")

</div>

Awesome! Thank you so much for this news!

---

<div class="post-metadata">

**Author:** ![cgifford](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cgifford/32/21484_2.png) [@cgifford](https://community.auth0.com/u/cgifford)\
**Post date:** [September 3, 2022, 1:21pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/151 "2022-09-03T13:21:20Z")

</div>

Best practice or not, I must say [Prasanth\_Reddy](https://community.auth0.com/u/Prasanth_Reddy) that is quite impressive code you built there.

---

<div class="post-metadata">

**Author:** ![kwhite](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kwhite/32/5983_2.png) [@kwhite](https://community.auth0.com/u/kwhite)\
**Post date:** [September 9, 2022, 1:52am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/152 "2022-09-09T01:52:01Z")

</div>

Thanks for your article, i was able to get my Blazor WASM project up and running and my controller APIs are secured per your instructions. What i am missing is on the Authorized controller, my user is a claims identity but none of my profile information carried over. i looked through the claims, no email, no username, etc … what am i missing. On the server how to i know who is calling the API besides the Auth0 internal id? Do i have to add something to the token on the client side to send the additional profile claims? Thanks in advance

---

<div class="post-metadata">

**Author:** ![andrea.chiarelli](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andrea.chiarelli/32/16034_2.png) [@andrea.chiarelli](https://community.auth0.com/u/andrea.chiarelli)\
**Post date:** [September 12, 2022, 9:14am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/153 "2022-09-12T09:14:08Z")

</div>

Thank you for reading my article. I’m glad you were able to secure your application following the article’s instructions.

Regarding your questions, you are using an access token to call your protected API. By nature, an access token conveys authorization information, while an ID token contains identity information.  
Authorization information is everything your API needs to make authorization decisions: it can be permissions or roles, but sometimes, like maybe in your case, identity data.  
Please, take a look at [this article to learn more about the differences between ID and access tokens](https://auth0.com/blog/id-token-access-token-what-is-the-difference/).

First, I suggest analyzing if you really need identity data to make authorization decisions. Maybe you just need permissions. In this case, check out [this article about how to deal with permission-based authorization](https://auth0.com/blog/permission-based-security-aspnet-webapi/) and [this one about role-based authorization](https://auth0.com/blog/role-based-authorization-for-aspnet-webapi/).

If you actually need identity information to make authorization decisions, you can add this data to your access token as custom claims using an [Auth0 Action](https://auth0.com/blog/introducing-auth0-actions/). Check out [this document](https://auth0.com/docs/get-started/apis/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token) to learn how to do it.

I hope I’ve given you useful directions.

---

<div class="post-metadata">

**Author:** ![kwhite](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kwhite/32/5983_2.png) [@kwhite](https://community.auth0.com/u/kwhite)\
**Post date:** [September 13, 2022, 1:23am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/154 "2022-09-13T01:23:42Z")

</div>

Thanks for responding.

First let me explain my use case. Pretty straight forward … Blazor WASM doing CRUD operations to a database via API Controllers.

The caveat is our database context in the controller has an override to SaveChanges which accepts a username as a parameter. By examining the Change Tracking of the context we automatically create an audit log of what the user is committing to the database.

Now I am using Actions on my Auth0 tenant to append the username to the identity.

The picture below shows all the claims available at the client and controller. Redacted a bit to obscure real end points.

You can see my ‘username’ of ‘leftdogxxxx’ was added under the custom claim and visible on the client. User.Identity.Name on the Client also shows “Kevin White”

On the server API controller, Identity.Name is the internal “auth0|617xxxxxxxx”

I’m sure I’m missing something simple. But I would really like to include the claim “[https://auth.mydomain.com/username”](https://auth.mydomain.com/username%E2%80%9D) from the client to be passed to the API controller. I feel it probably has to be done with an inherited BaseAddressAuthorizationMessageHandler

Or is the solution on the API to use the ‘auth0|617xxxxxxxx’ id, and make a call back to Auth0 to get the username, but that seems like a lot of redundant calls.

Just looking for the best practice. Any help is appreciated

 ![auth0](https://global.discourse-cdn.com/auth0/original/3X/c/4/c49c49fa7d5688ca0643f4037461d08eba731766.png)

[Previous page](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661.md?page=6)

[Next page](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661.md?page=8)
