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

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [February 5, 2021, 1:52pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/92 "2021-02-05T13:52:08Z")

</div>



---

<div class="post-metadata">

**Author:** ![paulkiddybytes](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paulkiddybytes/32/12809_2.png) [@paulkiddybytes](https://community.auth0.com/u/paulkiddybytes)\
**Post date:** [February 10, 2021, 6:16am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/93 "2021-02-10T06:16:52Z")

</div>

Been battling with Blazor for a while now. Finally getting around to securing a site with Auth0.  
All goes well, can login and get the UI to change accordingly.

… but…

The logout process is a bit strange. Tried a number of approaches but always get the “logout-failed” call-back. Most recently with the error “The logout was not initiated from within the page.”

Really need to understand those final vital steps of what is the correct approach for logging out of Auth0 also.

---

<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 11, 2021, 11:41am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/94 "2021-02-11T11:41:18Z")

</div>

Hi @paulkiddybytes,  
The error you are reporting is strange. I know there are still issues with the logout process in Blazor WASM, as mentioned in the article and as reported by a few readers in the comment above.  
However, they don’t report any failure.  
Can you give me more information about your context (e.g. [requests and responses affected by the error](https://auth0.com/docs/troubleshoot/generate-and-analyze-har-files))?  
Also, keep in mind that the article targets .NET 3.1. It has not been tested on .NET 5.

If you want to learn more on the logout process in Auth0, please, [check out this document](https://auth0.com/docs/logout).

---

<div class="post-metadata">

**Author:** ![paul.wood](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paul.wood/32/27727_2.png) [@paul.wood](https://community.auth0.com/u/paul.wood)\
**Post date:** [February 20, 2021, 2:40pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/95 "2021-02-20T14:40:51Z")

</div>

Hi, great article!

I’ve just followed the tutorial and successfully integrated Auth0 into my new Blazor app. Also, I added an Email Domain Whitelist rule which seems to work as expected, however, whenever the rule fails it always just redirects to the following url: "/authentication/login-failed?message=There%20was%20an%20error%20signing%20in. I need to know whether the rule failed due to an invalid domain or the just that email has not been verified. Do you know if there is a way to get the callback to tell me the reason for the failure? Also, do you know where is the message “There was an error signing in” coming from?

---

<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, 2021, 11:02am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/96 "2021-02-22T11:02:43Z")

</div>

Hi Paul,  
Thank you for appreciating my article 🙂

Regarding your request, does your rule throw an exception [as in this example](https://auth0.com/docs/compliance/gdpr/gdpr-conditions-for-consent#disable-login-for-flagged-users)?  
As far as I know, the error message should be returned to the client.

“There was an error signing in” message is the [builtin error message from Blazor WASM](https://github.com/dotnet/aspnetcore/blob/648c15dbe90fb8c113f7c6b4adeb40d9e10494f6/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts#L172-L189).

---

<div class="post-metadata">

**Author:** ![christian.weyer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/christian.weyer/32/28161_2.png) [@christian.weyer](https://community.auth0.com/u/christian.weyer)\
**Post date:** [March 14, 2021, 5:19pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/97 "2021-03-14T17:19:19Z")

</div>

Did you ever solve this @paulkiddybytes ? I am getting the same error.  
.cc @andrea.chiarelli

---

<div class="post-metadata">

**Author:** ![itbeard](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/itbeard/32/28382_2.png) [@itbeard](https://community.auth0.com/u/itbeard)\
**Post date:** [March 25, 2021, 11:44pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/98 "2021-03-25T23:44:07Z")

</div>

Hi folks!  
Thx for this manual.  
I faced one problem that was described here: [[Blazor][Wasm] Set oidc Authentication Options in local storage · Issue #20574 · dotnet/aspnetcore · GitHub](https://github.com/dotnet/aspnetcore/issues/20574)  
The problem is that the oidc response is saved in the session storage by default and I haven’t find a way to configure it. I have to authenticate every time I open a new tab.  
Did smbd face this issue too?

---

<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:** [March 26, 2021, 9:12am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/99 "2021-03-26T09:12:04Z")

</div>

Hi @itbeard,  
Welcome to the Auth0 Community.

As mentioned in the issue thread, storing the ID token in the local storage is not secure. Actually, even storing it in the session storage is not so secure.  
You can learn more on token storage on the browser by reading the following docs:

- [Browser local storage scenarios](https://auth0.com/docs/tokens/token-storage?#browser-local-storage-scenarios)
- [Secure Browser Storage: The Facts](https://auth0.com/blog/secure-browser-storage-the-facts/)

Regarding the specific issue, it seems [they solved the issue with a silent authentication approach](https://github.com/dotnet/aspnetcore/issues/20574#issuecomment-610076033). Did you try with an updated version of Blazor?

---

<div class="post-metadata">

**Author:** ![itbeard](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/itbeard/32/28382_2.png) [@itbeard](https://community.auth0.com/u/itbeard)\
**Post date:** [March 26, 2021, 12:54pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/100 "2021-03-26T12:54:16Z")

</div>

Hello!  
Thx for your reply.  
I use the last version of Blazor that included in .NET5.  
Authentication works while I refresh the page, but non when I one page in the new tab, unfortunately.  
Looks like the hidden iframe approach does not work in my case for some reason …

---

<div class="post-metadata">

**Author:** ![paulkiddybytes](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paulkiddybytes/32/12809_2.png) [@paulkiddybytes](https://community.auth0.com/u/paulkiddybytes)\
**Post date:** [March 28, 2021, 10:40am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/101 "2021-03-28T10:40:01Z")

</div>

Nope. I’m stuck with the process of logging out. I’ve tried all kinds of stuff. The logout URL doesn’t return properly and the browser seems to cache the logged in user. Simply no way to properly and reliably logout a user. Doesn’t help that the whole stack seems really buggy.

---

<div class="post-metadata">

**Author:** ![MLogdberg](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mlogdberg/32/12776_2.png) [@MLogdberg](https://community.auth0.com/u/MLogdberg)\
**Post date:** [April 21, 2021, 9:08am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/102 "2021-04-21T09:08:23Z")

</div>

Any luck in this topic? I have the same issue

---

<div class="post-metadata">

**Author:** ![Tim\_Bassett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tim_bassett/32/28878_2.png) [@Tim\_Bassett](https://community.auth0.com/u/Tim_Bassett)\
**Post date:** [April 21, 2021, 1:50pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/103 "2021-04-21T13:50:12Z")

</div>

I am using this example, my code below.

The token.Value is very small, like it is just an token code. How do I get an access token that includes “aud”,“iss”, etc?

```
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {

        try
        {
            var accessToken = await this._tokenProvider.RequestAccessToken(new AccessTokenRequestOptions());
            if (accessToken.TryGetToken(out AccessToken token))
            {
                request.Headers.Add("Authorization", $"Bearer {token.Value}");
                return await base.SendAsync(request, cancellationToken);

            }
            else
            {
                throw new InvalidOperationException("Unable to get token.");

            }

        }
        catch (System.Exception e)
        {
            Console.WriteLine(e.ToString());
            throw;
        }
    }

```

---

<div class="post-metadata">

**Author:** ![Tim\_Bassett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tim_bassett/32/28878_2.png) [@Tim\_Bassett](https://community.auth0.com/u/Tim_Bassett)\
**Post date:** [April 21, 2021, 2:21pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/104 "2021-04-21T14:21:57Z")

</div>

I can see the calls in fiddler to:  
[https://tjb.auth0.com/authorize?client\_id=XXX](https://tjb.auth0.com/authorize?client_id=XXX)  
[https://tjb.auth0.com/oauth/token](https://tjb.auth0.com/oauth/token)  
[https://tjb.auth0.com/userinfo](https://tjb.auth0.com/userinfo)

and they are all successful…

and the [https://tjb.auth0.com/oauth/token](https://tjb.auth0.com/oauth/token) response includes a json payload with the access\_token and the id\_token. I need the value of id\_token. How do I get the value of id\_token?

---

<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 7, 2021, 4:46pm UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/105 "2021-05-07T16:46:26Z")

</div>

Hey folks, this blog post has been revamped to .NET 5!

---

<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 10, 2021, 9:47am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/106 "2021-05-10T09:47:53Z")

</div>

Woooohoo great news!

---

<div class="post-metadata">

**Author:** ![Prasanth\_Reddy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prasanth_reddy/32/20872_2.png) [@Prasanth\_Reddy](https://community.auth0.com/u/Prasanth_Reddy)\
**Post date:** [May 14, 2021, 3:43am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/107 "2021-05-14T03:43:54Z")

</div>

Hi @Tim_Bassett, Did you get the solution for that? I am also facing the same issue.  
I have configured everything to make HTTP requests with auth token but my API calls were failing due to an invalid token. access\_token is attaching to the requests instead of id\_token. id\_token is actual JWT.

---

<div class="post-metadata">

**Author:** ![Tim\_Bassett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tim_bassett/32/28878_2.png) [@Tim\_Bassett](https://community.auth0.com/u/Tim_Bassett)\
**Post date:** [May 14, 2021, 4:11am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/108 "2021-05-14T04:11:26Z")

</div>

We ended up getting rid of all the manual manipulation of the Token by using the BaseAddressAuthorizationMessageHandler. We also had CORS issues, so inevitably getting everything onto the same domain (both the web and the api) got get rid of the CORS issues, which yielded the opportunity to just use the BaseAddressAuthorizationMessageHandler. Using the BaseAddressAuthorizationMessageHandler got us out of the manually handling the token business, and then everything just worked.

We do use the AuthorizationMessageHandler to do a bit of manual token handling for development against the localhost api (different port). I believe using the AddHttpClient along with the other pattern inside the delegate got us home on that front…

```
                        builder.Services.AddHttpClient<TClient>(client => builder.Configuration.Bind("HttpClient", client))
                        .AddHttpMessageHandler<AuthorizationMessageHandler>();
                    builder.Services.AddTransient<AuthorizationMessageHandler>(sp =>
                    {
                        // 👇 Get required services from DI.
                        var provider = sp.GetRequiredService<IAccessTokenProvider>();
                        var naviManager = sp.GetRequiredService<NavigationManager>();

                        // 👇 Create a new "AuthorizationMessageHandler" instance,
                        // and return it after configuring it.
                        var handler = new AuthorizationMessageHandler(provider, naviManager);
                        handler.ConfigureHandler(authorizedUrls: new[] {builder.Configuration["HttpClient:BaseAddress"]});
                        return handler;
                    });

```

---

<div class="post-metadata">

**Author:** ![Prasanth\_Reddy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prasanth_reddy/32/20872_2.png) [@Prasanth\_Reddy](https://community.auth0.com/u/Prasanth_Reddy)\
**Post date:** [May 14, 2021, 4:15am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/109 "2021-05-14T04:15:03Z")

</div>

I am not sure whether it is an audience issue or some other issue. Because I have verified the login call back URL and noticed both the tokens (access\_token and id\_token) are coming but only access token mapping to HTTP requests.  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/8/4/8458082d70aecbeb5e4cc0c8cd8f6d01b315989f.png)

---

<div class="post-metadata">

**Author:** ![Prasanth\_Reddy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prasanth_reddy/32/20872_2.png) [@Prasanth\_Reddy](https://community.auth0.com/u/Prasanth_Reddy)\
**Post date:** [May 14, 2021, 4:54am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/110 "2021-05-14T04:54:45Z")

</div>

Hi @Tim_Bassett, I have tried with your code but no luck. Still access\_token passing as Bearer token in request headers.

![image](https://global.discourse-cdn.com/auth0/original/3X/3/0/30431f741f07764a8d840a3b6f6bc006509142a4.png)

---

<div class="post-metadata">

**Author:** ![Tim\_Bassett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tim_bassett/32/28878_2.png) [@Tim\_Bassett](https://community.auth0.com/u/Tim_Bassett)\
**Post date:** [May 14, 2021, 5:27am UTC](https://community.auth0.com/t/securing-blazor-webassembly-apps/46661/111 "2021-05-14T05:27:43Z")

</div>

Sorry, I didn’t fully comprehend what you were trying to accomplish…

In our backend ([asp.net](http://asp.net)) we are using middleware to get the token from the access token.

Some of this is voodoo to me, but I believe the “spell” is in this:

```
            string domain = $"https://{this.Configuration["Auth0:Domain"]}/";
        services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.Authority = domain;
                options.Audience = this.Configuration["Auth0:Audience"];
                // If the access token does not have a `sub` claim, `User.Identity.Name` will be `null`. Map it to a different claim by setting the NameClaimType below.
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = ClaimTypes.NameIdentifier
                };
            });

```

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

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