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

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [October 29, 2020, 9:19pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/46 "2020-10-29T21:19:46Z")

</div>

I followed the steps, and can successfully sign in. But, when I add the [Authorize] attribute to a page I get a 401-UnAuthorized response.

---

<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:** [October 30, 2020, 8:44am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/47 "2020-10-30T08:44:35Z")

</div>

Thanks for reporting that! We’ll look into that shortly!

---

<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:** [November 3, 2020, 9:18am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/49 "2020-11-03T09:18:36Z")

</div>

Hey @mjones,  
Are you referring to the page attribute (`@attribute [Authorize]`) added to `Client/Pages/QuizViewer.razor`? If so, it really strange because this authorization attribute shouldn’t involve the server 🤔  
Did you try to download the full project from GitHub and run it? Just to understand if something went wrong while building your app.  
Please, let me know.

---

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [November 4, 2020, 4:24pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/50 "2020-11-04T16:24:47Z")

</div>

I did download the full project from hithub, and then I setup an Auth0 application to test it.

Sorry, I misspoke when I said “add the [Authorize] attribute to a page”. Let me try to do a better job of explaining the problem…

QuizManager works fine for me with @attribute [Authorize] on the QuizViewer page and without the [Authorize] attribute on the QuizController, but with the @attribute [Authorize] on the QuizViewer page and the [Authorize] attribute on QuizController, I get a 401-UnAuthorized error.

---

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [November 4, 2020, 10:42pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/51 "2020-11-04T22:42:24Z")

</div>

Thequiz request returns "www.Authenticate: Bearer error=‘invalid\_token’ in the response header.

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

---

<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:** [November 5, 2020, 8:23am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/52 "2020-11-05T08:23:07Z")

</div>

Thanks for providing that context @mjones!

---

<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:** [November 6, 2020, 2:41pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/53 "2020-11-06T14:41:44Z")

</div>

Hi @mjones,  
Thank you for providing more detailed information.  
At first look, it seems like you missed to define the default audience on the Auth0 side. Have you defined it?  
The article explains what to do and why in [Calling the Protected API](https://auth0.com/blog/securing-blazor-webassembly-apps/#Calling-the-Protected-API).  
Be sure that the audience you define in your Auth0 Dashboard is exactly the same you are using in your `appsettings.json`.  
If you already defined your default audience, please, take a look at the logs in your Auth0 Dashboard to get any other information that can help us to investigate.

Please, let me know.

---

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [November 7, 2020, 9:31pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/54 "2020-11-07T21:31:04Z")

</div>

[WASM Auth Problem.pdf](https://community.auth0.com/uploads/short-url/pzJbkrekc5DAPwMY19CQTXOwStA.pdf) (283.4 KB)

---

<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:** [November 9, 2020, 1:49pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/55 "2020-11-09T13:49:49Z")

</div>

Hey @mjones, your code looks ok.  
My only concern is about this:

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

Did you define the API identifier only in the API section of your Auth0 Dashboard?  
**Did you also assign it as the Default Audience in your tenant settings?**

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

The error message you get leads me to think you didn’t.  
Please, confirm me if you defined `https://quizmgr_api` as the Default Audience for your tenant.  
Otherwise, I’m afraid I need all the HTTP requests exchanged with Auth0 at the login step to see which parameters are passed.

---

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [November 9, 2020, 5:51pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/56 "2020-11-09T17:51:37Z")

</div>

That was the problem - setting the tenant’s Default Audience got it working. I appreciate your assistance. Thanks for hanging in there with me.

So this means one Api per tenant, yes?

---

<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:** [November 10, 2020, 8:50am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/57 "2020-11-10T08:50:08Z")

</div>

Happy to hear it solved your problem!

> So this means one Api per tenant, yes?

Unfortunately, yes! This is the current limitation for Blazor WASM applications.  
As said before, the [Calling the Protected API](https://auth0.com/blog/securing-blazor-webassembly-apps/#Calling-the-Protected-API) section explains the details behind this workaround.

---

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [November 11, 2020, 11:57pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/58 "2020-11-11T23:57:55Z")

</div>

Do you have a .Net Core 5 example for securing a WASM client with Auth0?

---

<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:** [November 12, 2020, 9:34am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/59 "2020-11-12T09:34:15Z")

</div>

Unfortunately, not yet, @mjones.

---

<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:** [November 12, 2020, 11:38am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/60 "2020-11-12T11:38:37Z")

</div>

Once we have that eventually we’ll let you know @mjones!

---

<div class="post-metadata">

**Author:** ![mjones](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mjones/32/12764_2.png) [@mjones](https://community.auth0.com/u/mjones)\
**Post date:** [November 26, 2020, 12:37am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/61 "2020-11-26T00:37:49Z")

</div>

Ok, thanks. I’m looking forward to it…

---

<div class="post-metadata">

**Author:** ![soend](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/soend/32/25883_2.png) [@soend](https://community.auth0.com/u/soend)\
**Post date:** [November 27, 2020, 1:07pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/62 "2020-11-27T13:07:15Z")

</div>

Has anybody using Auth0 and Blazor webassembly figured out what could be the issue with logout? It still seems to be broken. I looked around and i didnt find anybody else having any issues with logging out, what makes me think its more of Auth0 issue.  
Any regarding to this is help welcome.

---

<div class="post-metadata">

**Author:** ![soend](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/soend/32/25883_2.png) [@soend](https://community.auth0.com/u/soend)\
**Post date:** [December 1, 2020, 8:55am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/64 "2020-12-01T08:55:41Z")

</div>

I think i figured it out why the logout doesnt work every time. It seems to be important to call the Auth0 logout endpoint before i navigate to `authentication/logout` in blazor app.

So i changed the `BeginSignOut` function in AccessControl.razor to look like this:

> private async Task BeginSignOut(MouseEventArgs args)  
> {  
> await SignOutManager.SetSignOutState();
> 
> ```
> var authority = (string)Configuration["Auth0:Authority"];
> var clientId = (string)Configuration["Auth0:ClientId"];
> 
> Navigation.NavigateTo($"{authority}/v2/logout?client_id={clientId}");
> 
> ```
> 
> }

And also removed the `<LogOut>` block from Authentication.razor. So it looks now like this:  
@page “/authentication/{action}”  
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

```
<RemoteAuthenticatorView Action="@Action" />

@code{
    [Parameter] public string Action { get; set; }
}

```

And the last thing was to set `https://localhost:5001/authentication/logout` as the `Allowed Logout URLs` in the Auth0 Applications settings.

---

<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 1, 2020, 9:51am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/66 "2020-12-01T09:51:21Z")

</div>

Hi @soend,  
Thank you so much for joining the Auth0 Community and sharing your thoughts on the logout issue.  
I tried the solution you suggested, but I’m not sure those changes are enough to solve the issue.  
First, I think that the code for `Authentication.razor` should still contain the `RemoteAuthenticatorView` component, as follows:

```auto
@page "/authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

<RemoteAuthenticatorView Action="@Action">
</RemoteAuthenticatorView>

@code{
    [Parameter] public string Action { get; set; }
}

```

Otherwise, you will be able neither to log in.

Second, by moving the navigation to the Auth0 logout endpoint within the `BeginSignOut ` method, I’m not able to logout at all. If it works on your side, are there chances you made some other change?

---

<div class="post-metadata">

**Author:** ![soend](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/soend/32/25883_2.png) [@soend](https://community.auth0.com/u/soend)\
**Post date:** [December 1, 2020, 10:33am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/67 "2020-12-01T10:33:47Z")

</div>

Hello, @andrea.chiarelli  
Yeah sorry my copy paste went wrong. I still have the `<RemoteAuthenticatorView Action="@Action">` in the `Authentication.razor`. Sorry about that, i fixed it.

These are the only changes i made.  
Here’s the repo: [https://github.com/soend/QuizManagerClientHosted](https://github.com/soend/QuizManagerClientHosted)

---

<div class="post-metadata">

**Author:** ![karam94](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/karam94/32/12789_2.png) [@karam94](https://community.auth0.com/u/karam94)\
**Post date:** [December 6, 2020, 6:36pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/68 "2020-12-06T18:36:58Z")

</div>

Hi @andrea.chiarelli ,

Fascinatingly, I am now running in to an issue.  
I followed this tutorial: [How to Secure Blazor WASM Applications with Auth0](https://auth0.com/blog/securing-blazor-webassembly-apps/)

I reached the stage where I was getting the 401 errors when trying to connect to my API, which required adding a Default Audience in my Tenant Settings.

Unfortunately, as soon as I add a Default Audience, my Blazor client breaks. I can click login, and it logs in successfully (as per the Auth0 logs), however when it redirects back to my app, I get a 401 unauthorized error on the “https://.auth0.com/userinfo” call that Blazor seems to make. Removing the Default Audience on my tenant fixes it, but obviously I still then have the same 401 issue with my IP due to the token issue.

The 401 Response headers contain this under www-authenticate:  
`Bearer realm="Users", error="invalid_token", error_description="The access token signature could not be validated. A common cause of this is requesting multiple audiences for an access token signed with HS256, as that signature scheme requires only a single recipient for its security. Please change your API to employ RS256 if you wish to have multiple audiences for your access tokens"`

I’m really struggling to understand what’s going wrong and where, bar the obvious where I’m currently using HS256 to ensure I have access to the issuer signing key. Could this be the cause of the problem?

Logging in as far as I can tell is all on the client, so I’m confused as to where the issue could lie. Once I can actually log in with a Default Audience set, I should then be able to test the API (and assumingly, only then does the authentication set up in the API become relevant).

Any ideas?

Thanks

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

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