# 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:** 8

<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 13, 2022, 9:22am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/155 "2022-09-13T09:22:45Z")

</div>

> [@kwhite](#):
>
> Now I am using Actions on my Auth0 tenant to append the username to the identity.

As far as I understand, your Action adds the custom claim `https://auth.mydomain.com/username` to the ID token with a statement like the following:

```javascript
api.idToken.setCustomClaim(`${namespace}/username`, event.user.username);

```

You need to add the same custom claim to the access token with a statement like the following:

```javascript
api.accessToken.setCustomClaim(`${namespace}/username`, event.user.username);

```

This way, you will find the custom claim `https://auth.mydomain.com/username` on the server side as well.

Remember: the API receives the access token, not the ID token, which is used to populate the client-side identity.

I hope this clarifies.

---

<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:15pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/156 "2022-09-13T13:15:39Z")

</div>

That’s exactly what i needed thank you so much. Such a simple mistake. Sometimes all you need is that extra set of eyes to see. 😄

---

<div class="post-metadata">

**Author:** ![magoc.dan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/magoc.dan/32/34280_2.png) [@magoc.dan](https://community.auth0.com/u/magoc.dan)\
**Post date:** [September 14, 2022, 8:52pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/157 "2022-09-14T20:52:10Z")

</div>

Hello, I’m having a headache trying to get my WASM application to authenticate through auth0.

I’ve followed the tutorial.

These are my client’s Program.cs and appsettings.json files:

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

These are the api’s Program.cs and appsettings.json files

 ![image](https://global.discourse-cdn.com/auth0/original/3X/6/7/678a50b34719638d3021f3fb17501a592a249aa4.png)

I’ve set up the SPA and API applications in the Auth0 dashboard. When running the application, I can successfully log in and I see the pages which are attributed as Authorized only when logged in.

However, the calls to my [Authorized] controller endpoints always result in 401, which I’ve noticed is a reoccurring problem here. I’m running .NET 6 and despite that I’ve tried setting up the tenant’s default audience, which did not help.

The request’s header does contain a bearer:

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

If anyone would help me figure the mistake I keep doing, please, I would highly appreciate it.

---

<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 15, 2022, 8:06am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/158 "2022-09-15T08:06:43Z")

</div>

Hi @magoc.dan,  
Welcome to the Auth0 Community!

I noticed that your API uses the following code to register authentication and authorization middleware:

```csharp
app.UseAuthorization();
app.UseAuthentication();

```

The middleware registration order is important. You should first register the authentication middleware and then the authorization middleware, as proposed in the tutorial. In other words, your code should be as follows:

```csharp
app.UseAuthentication();
app.UseAuthorization();

```

Check out [this article to learn more about securing an ASP.NET Web API](https://auth0.com/blog/aspnet-web-api-authorization).

---

<div class="post-metadata">

**Author:** ![magoc.dan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/magoc.dan/32/34280_2.png) [@magoc.dan](https://community.auth0.com/u/magoc.dan)\
**Post date:** [September 15, 2022, 8:33am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/159 "2022-09-15T08:33:33Z")

</div>

That… actually worked. Thank you very much for pointing it out!

---

<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 15, 2022, 11:04am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/160 "2022-09-15T11:04:53Z")

</div>

You are welcome! 🙂

---

<div class="post-metadata">

**Author:** ![tnking83](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tnking83/32/22932_2.png) [@tnking83](https://community.auth0.com/u/tnking83)\
**Post date:** [November 23, 2022, 9:10pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/161 "2022-11-23T21:10:21Z")

</div>

This was my issue. I missed this line and couldn’t figure out why my access token was invalid! Thanks a bunch. 😀

---

<div class="post-metadata">

**Author:** ![obiwan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/obiwan/32/41625_2.png) [@obiwan](https://community.auth0.com/u/obiwan)\
**Post date:** [November 30, 2022, 9:54pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/162 "2022-11-30T21:54:33Z")

</div>

I’ve been following the tutorial at Auth0 for securing a Blazor WASM and API with Aut0, which is found here → [How to Secure Blazor WASM Applications with Auth0](https://auth0.com/blog/securing-blazor-webassembly-apps/)

Securing the app works fine, but adding the API gives me issues. As soon as I add the authenticate attribute to the API Controller it results in this:

> fetchdata:1 Access to fetch at ‘[https://localhost:7226/weatherforecast](https://localhost:7226/weatherforecast)’ from origin ‘[https://localhost:7298](https://localhost:7298)’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

I’ve added a policy to allow the app in the program.cs

```auto
builder.Services.AddCors(options =>
{
  options.AddPolicy("Open", builder => builder.WithOrigins("https://localhost:7298").AllowAnyMethod().AllowAnyHeader());
});

I've played around with the program.cs and also added app.UseCors before authentication/authorization (as a provided solution I found online), which then results in another issue.

> Failed to load resource: the server responded with a status of 401 ()
>
>
>
> blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0. System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

That error seems to indicate an issue with the bearertoken not being set, but it is setup using BaseAddressAuthorizationMessageHandler.

```

builder.Services.AddHttpClient(“APIClient”, client =\>  
{  
client.BaseAddress = new Uri(“[https://localhost:7226](https://localhost:7226)”);  
client.DefaultRequestHeaders.Clear();  
client.DefaultRequestHeaders.Add(HeaderNames.Accept, “application/json”);  
}).AddHttpMessageHandler();

```auto

I've added the project to GitHub, if more details on the code is of interest https://github.com/obrungot/BlazorAuthenticationPlayGround.git
```

---

<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:** [December 12, 2022, 9:51am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/163 "2022-12-12T09:51:32Z")

</div>

Hey @obiwan,  
Welcome to the auth0 Community! 👋

The article proposes the _ASP.NET Core hosted_ approach to publish your Blazor WASM app (see [here](https://auth0.com/blog/securing-blazor-webassembly-apps/#Building-a-Blazor-WebAssembly-Application)). This means that the Blazor WASM app comes from the same domain as the Web API runs, and you don’t need to set up CORS.  
Honestly, if you don’t have any specific reasons to publish your Web API on a different domain, I would stick with the _ASP.NET Core hosted_ approach.

---

<div class="post-metadata">

**Author:** ![Ditchford](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ditchford/32/13599_2.png) [@Ditchford](https://community.auth0.com/u/Ditchford)\
**Post date:** [January 19, 2023, 7:11am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/164 "2023-01-19T07:11:42Z")

</div>

Hello

I followed the excellent article by @andrea.chiarelli on securing Blazor Wasm with Auth0:

> **[How to Secure Blazor WASM Applications with Auth0](https://auth0.com/blog/securing-blazor-webassembly-apps/)**
>
> Learn how to secure Blazor WebAssembly applications with Auth0.

I have been able to secure our application, and everything has been running without a problem. There is one issue that remains that I have not been able to resolve. The following message is output to the console multiple (approximately 50 times) during log in:

Authorization failed. These requirements were not met:  
DenyAnonymousAuthorizationRequirement: Requires an authenticated user

I’m guessing it’s something to do with silent v interactive login.

Does anyone have any ideas why I am getting this message.

Many 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:** [January 23, 2023, 11:43am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/165 "2023-01-23T11:43:17Z")

</div>

Hi @Ditchford ,  
I tried to figure out what is happening with your application but honestly I’m afraid I can’t do much.

As far as I know, this message is shown when your application comes across a `<NotAuthorized>` element and in the Blazor WASM case, it should happen on the client side (i.e., the server is not involved).

For example, in the sample project of my article, this message is shown only once, before the user authentication. This is correct because the current not authenticated user is attempting to access the quiz page.  
If you switch between the home page and the quiz page before authenticating, you can see this message shown in the JavaScript console for each attempt to access the quiz page. No request is submitted to the server though.

I hope this may help to identify what is happening in your application.

---

<div class="post-metadata">

**Author:** ![Ditchford](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ditchford/32/13599_2.png) [@Ditchford](https://community.auth0.com/u/Ditchford)\
**Post date:** [January 23, 2023, 11:46pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/166 "2023-01-23T23:46:34Z")

</div>

Hey @andrea.chiarelli. Thanks for that. It made me go back and re-examine my login workflow. The tip about the tags was spot on. The main issue (I think) is that I have used the tags , , in several places in my MainLayout and each of these is generating a message during login. Add to that the fact that Blazor appears to render MainLayout twice and a strange bug where the following code does not initially return the authenticated user:

AuthenticationState authenticationState = await authenticationStateTask;  
ClaimsPrincipal user = authenticationState.User;  
string emailaddress = user.Identity.Name ?? “”;

In the above when the email address is empty, I reload MainLayout and on the second pass the email is populated.

The combination of all the above explains the multiple messages, and why I have to refactor my login code.

Thank you

---

<div class="post-metadata">

**Author:** ![spmoran-blazor](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/spmoran-blazor/32/44715_2.png) [@spmoran-blazor](https://community.auth0.com/u/spmoran-blazor)\
**Post date:** [February 16, 2023, 9:30pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/167 "2023-02-16T21:30:18Z")

</div>

That was a great articla. Is there an article on securing Azure Static Web Apps (serverless) build with Blazor WASM (not hosted) using Okta? Thank you.

---

<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:** [February 17, 2023, 3:52pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/168 "2023-02-17T15:52:14Z")

</div>

Hi @spmoran-blazor,  
Welcome to the Auth0 Community! 👋

You can read [this article](https://developer.okta.com/blog/2020/09/30/blazor-webassembly-wasm-dotnetcore) to learn how to secure a Blazor WASM app with Okta.

For deployment to Azure Static Web Apps, we don’t currently have a specific article. You can refer to the [Microsoft documentation](https://learn.microsoft.com/en-us/azure/static-web-apps/deploy-blazor).  
Of course, keep in mind that you can only deploy the WASM application to Azure Static Web Apps.  
I hope this helps.

---

<div class="post-metadata">

**Author:** ![cashbucket](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@cashbucket](https://community.auth0.com/u/cashbucket)\
**Post date:** [February 21, 2023, 8:14pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/169 "2023-02-21T20:14:04Z")

</div>

Hi Andrea,

We have an issue with passing a query parameter from Blazor to Auth0. It seems to be similar to the issue here that said it has been fixed but doesn’t seem to work in our case [[Blazor][Wasm] Dynamic and extensible authentication requests · Issue #42580 · dotnet/aspnetcore · GitHub](https://github.com/dotnet/aspnetcore/issues/42580)

We have an invite code that is relevant to our Blazor application that needs to be passed to create a new tenant in the app. Blazor seems to be unable to add the invite code to the query parameter when redirecting to the auth0 login page. On login success the invite code will disappear after redirecting back to our app.

We have tried with a vanilla blazor app on macos and windows and the problem still exists. Any suggestions or advice appreciated.

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:** [February 22, 2023, 11:50am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/170 "2023-02-22T11:50:01Z")

</div>

Hey @cashbucket,  
Welcome to the Auth0 Community!

I’m not sure I correctly understand your problem.  
To pass any additional parameter to Auth0 during the authentication, you should add them to the OIDC configuration as in the following example:

```plaintext
options.ProviderOptions.AdditionalProviderParameters.Add("myparam", "myvalue");

```

This works, otherwise you could not even pass the `audience` parameter, for example.

Please, can you provide more details on what you are trying to do?  
What version of .NET are you using?  
What parameter are you trying to pass to Auth0?  
Can you share a sample code or more details on the flow you are implementing?

---

<div class="post-metadata">

**Author:** ![al-cashbucket](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@al-cashbucket](https://community.auth0.com/u/al-cashbucket)\
**Post date:** [February 23, 2023, 12:56am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/171 "2023-02-23T00:56:50Z")

</div>

Hi @andrea.chiarelli thank you for your response!

The mechanism that you pointed out works, however it’s only for static values. What we’re looking for is a way to pass custom parameters with dynamic values.

We have a system where a new user receives a link to our application to register with an invite code. We’re looking for a way to relay that invite code to auth0 so that we can use it in custom rules/hooks to the workflow. We would also like that invite code to be transmitted back to our application on login-callback.

Below is a snippet of our code extracting the `invite_code` value from our invite link query parameter and adding it to additional parameters on authentication redirect. However invite\_code parameter is not appended to the /authorize endpoint.

```auto
InteractiveRequestOptions requestOptions =
      new()
      {
          Interaction = InteractionType.SignIn,
          ReturnUrl = Navigation.Uri,
      };

  QueryHelpers.ParseQuery(Navigation.ToAbsoluteUri(Navigation.Uri).Query)
      .TryGetValue("invite_code", out var inviteCode);

  requestOptions.TryAddAdditionalParameter("invite_code", inviteCode);

  Navigation.NavigateToLogin("authentication/login", requestOptions);

```

What are we missing?

---

<div class="post-metadata">

**Author:** ![stevchik](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@stevchik](https://community.auth0.com/u/stevchik)\
**Post date:** [February 23, 2023, 3:53am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/172 "2023-02-23T03:53:02Z")

</div>

Having an issue connecting from Blazor WebAssembly to [ASP.net](http://ASP.net) Core backend api.

Able to login from BlazorWebAssembly and get a token. However, sending this topic to API returns:  
WWW-Authenticate: Bearer error=“invalid\_token”, error\_description=“The signature is invalid”

UI client and Backend API are not using same base URL. Client HttpClient is set up to use AuthorizationMessageHandler:

builder.Services.AddHttpClient(GlobalConstants.USERMANAGEMENT\_API, client =\> client.BaseAddress = new Uri(userServiceUrl))  
.AddHttpMessageHandler(sp =\> sp.GetRequiredService()  
.ConfigureHandler(  
authorizedUrls: new { userServiceUrl }));

Api is set up per starter guide:  
builder.Services.AddAuthentication(options =\>  
{  
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;  
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;  
}).AddJwtBearer(options =\>  
{  
options.Authority = AuthSettings.GetAuthority(builder.Configuration);  
options.Audience = AuthSettings.GetAudience(builder.Configuration);

```
});

```

I double checked that Authority and Audience match between client and api.  
Not using RBAC.

Any ideas to try?

Thank you

---

<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:** [February 23, 2023, 4:53pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/173 "2023-02-23T16:53:12Z")

</div>

Hi @al-cashbucket,  
I’ve done some tests using your code and it actually looks like it doesn’t work as expected.  
Your code looks correct. I checked out the [Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-7.0#customize-the-login-process) and it seems like this should be the correct approach. I feel like this is a bug in the Blazor authentication library.  
I suggest you open an issue with the Blazor team.  
In the meantime, I’ll keep investigating in case I missed something.

---

<div class="post-metadata">

**Author:** ![al-cashbucket](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@al-cashbucket](https://community.auth0.com/u/al-cashbucket)\
**Post date:** [February 23, 2023, 7:31pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/174 "2023-02-23T19:31:52Z")

</div>

Thanks @andrea.chiarelli

My theory at the moment is that the Microsoft Authentication library is making a request to /.well-known/openid-configuration to determine what the valid authentication request parameters and then automatically dropping any parameters that aren’t valid. I found that standard parameters such as prompt, login\_hint, display, etc works with the above code, but not anything custom. Is there anything we could configure on the auth0 side to allow it?

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

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