# What is Blazor? A Tutorial on Building Web Apps with Authentication

**URL:** <https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611>\
**Category:** Get Help\
**Tags:** dotnet, net-core, aspnet-core, .net, blazor\
**Created:** [November 11, 2019, 2:56pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611 "2019-11-11T14:56:06Z")\
**Posts on this page:** 20\
**Page:** 4

<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:** [July 4, 2020, 11:02am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/62 "2020-07-04T11:02:34Z")

</div>

Hey @Stef, thanks for your suggestions.  
The article about Blazor WASM and Auth0 integration will be published in a few days. It will include how to overcome the missing `audience` support you mentioned with a practical example.

---

<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:** [July 6, 2020, 7:04am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/63 "2020-07-06T07:04:56Z")

</div>

Thanks for the headsup Andrea!

---

<div class="post-metadata">

**Author:** ![sarmientodiego1979](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sarmientodiego1979/32/22592_2.png) [@sarmientodiego1979](https://community.auth0.com/u/sarmientodiego1979)\
**Post date:** [July 16, 2020, 7:21pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/64 "2020-07-16T19:21:27Z")

</div>

Hi! I have this Blazor server site:

[https://lenceria-7yeyjec4xa-de.a.run.app](https://lenceria-7yeyjec4xa-de.a.run.app)

If login in from the web, authentication works fine, but authentication fails if login from a mobile device (mobile browser). Please, if you can explain the difference, it would be very important for me.

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:** [July 17, 2020, 8:10am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/65 "2020-07-17T08:10:13Z")

</div>

Any error/log to share, @sarmientodiego1979?

---

<div class="post-metadata">

**Author:** ![sarmientodiego1979](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sarmientodiego1979/32/22592_2.png) [@sarmientodiego1979](https://community.auth0.com/u/sarmientodiego1979)\
**Post date:** [July 17, 2020, 1:47pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/66 "2020-07-17T13:47:00Z")

</div>

Thanks for answering.  
I was making changes to replay.

The throwing error is:

_An unhandled exception occurred while processing the request._  
_ **Exception: OpenIdConnectAuthenticationHandler: message.State is null or empty.** _  
_Unknown location_

_Exception: An error was encountered while handling the remote login._  
_Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()_

_Stack Query Cookies Headers Routing_  
_Exception: OpenIdConnectAuthenticationHandler: message.State is null or empty._

_Show raw exception details_  
_Exception: An error was encountered while handling the remote login._  
_Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()_  
_Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)_  
_Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)_  
_Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)_

_Show raw exception details_

Remember that from the **web it works fine, and from mobile phones it does not**.  
In case you want to enter: [http://lenceria-7yeyjec4xa-de.a.run.app/](http://lenceria-7yeyjec4xa-de.a.run.app/)

---

<div class="post-metadata">

**Author:** ![sarmientodiego1979](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sarmientodiego1979/32/22592_2.png) [@sarmientodiego1979](https://community.auth0.com/u/sarmientodiego1979)\
**Post date:** [July 18, 2020, 3:10pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/67 "2020-07-18T15:10:37Z")

</div>

I made it work. If anyone needs it, write me.

Thanks.

---

<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:** [July 20, 2020, 7:49am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/68 "2020-07-20T07:49:13Z")

</div>

Glad you have figured 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:** [July 20, 2020, 8:19am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/69 "2020-07-20T08:19:37Z")

</div>

Happy to hear this, @sarmientodiego1979  
Out of curiosity, was it a matter of Auth0 configuration or application code?

---

<div class="post-metadata">

**Author:** ![sarmientodiego1979](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sarmientodiego1979/32/22592_2.png) [@sarmientodiego1979](https://community.auth0.com/u/sarmientodiego1979)\
**Post date:** [July 20, 2020, 12:32pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/70 "2020-07-20T12:32:22Z")

</div>

It was a code problem. I am deploying Blazor in Google Cloud Run, so there were not many examples about it.

Specifically, the problem was in the http / https callback (I suppose that related to load balancing). To solve it I had to put the following code in the Configure method of Startup.cs:

app.UseForwardedHeaders (new ForwardedHeadersOptions  
{  
ForwardedHeaders = ForwardedHeaders.XForwardedProto  
});

Regards.

---

<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:** [July 20, 2020, 1:32pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/71 "2020-07-20T13:32:48Z")

</div>

@sarmientodiego1979 Thank you so much for sharing 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:** [July 22, 2020, 2:50pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/72 "2020-07-22T14:50:29Z")

</div>

Hey Community, 👋  
For those waiting for the tutorial about securing a Blazor WebAssembly client, it has been published:

> **[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.

Please, let me know your thoughts!

---

<div class="post-metadata">

**Author:** ![twheeler](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/twheeler/32/17979_2.png) [@twheeler](https://community.auth0.com/u/twheeler)\
**Post date:** [July 22, 2020, 2:58pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/73 "2020-07-22T14:58:35Z")

</div>

AWESOME! Thanks Andrea!

---

<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:** [July 22, 2020, 3:03pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/74 "2020-07-22T15:03:08Z")

</div>

Welcome to Auth0 Community @twheeler!

---

<div class="post-metadata">

**Author:** ![yuramag](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/yuramag/32/17177_2.png) [@yuramag](https://community.auth0.com/u/yuramag)\
**Post date:** [July 25, 2020, 7:07pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/75 "2020-07-25T19:07:54Z")

</div>

Andrea, thanks for the article, it’s been super helpful! After reading it, I’ve decided to switch my small dotnet core app from `IdentityServer` to using Auth0 instead. However, I am facing the following problems at the moment - any help/advice is greatly appreciated:

1. Since I had the users stored in the same DB with my other data, I used `UserManager<ApplicationUser>` to access user details so I could show their names, emails etc. in the UI. By users hear I don’t mean current user, but let’s say a list of users IDs recorded in `CreatedBy` column in some of my tables. After switching to Auth0, `UserManager` is no longer available as-is. What’s your recommendation around implementing a replacement functionality? Should I use Auth0’s management APIs to implement my own version of `UserManager`? If so, is there a ready to use NuGet package available?
2. Related question to above - I used to have User IDs stored as `Guid`. Seems like Auth0’s `sub` claims are not guids any longer - should I change the datatype of User ID columns to `string`s in my entities? What’s your recommendation around referencing users in SQL data stores?
3. In my app I used `IdentityServer` with pre-configured in-memory Test Client and Test User to run my integration tests by calling into `GetDiscoveryDocumentAsync()` and `RequestPasswordTokenAsync()`. What is the recommended approach to design integration testing harness in Auth0?

---

<div class="post-metadata">

**Author:** ![nathanuel81](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nathanuel81/32/13118_2.png) [@nathanuel81](https://community.auth0.com/u/nathanuel81)\
**Post date:** [July 26, 2020, 1:06am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/76 "2020-07-26T01:06:36Z")

</div>

Thanks for the guide, with a bit of tinkering I managed to get it working as expected.

Thought I would share some code I pulled together to centralise the Bearer token being added to requests in case anyone wants to do the same.

I created a custom message handler class like suggested in the Microsoft docs article. To add the bearer to each request I overrode the SendAsync method to get the access token and add it to each request.

```
public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
{
    private readonly IAccessTokenProvider _provider;

    public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
        NavigationManager navigationManager)
        : base(provider, navigationManager)
    {
        ConfigureHandler(new[] { "api uri" });

        _provider = provider;
    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var accessTokenResult = await _provider.RequestAccessToken();

        if (accessTokenResult.TryGetToken(out var token))
        {
            //add token to request
            if (!string.IsNullOrWhiteSpace(token.Value))
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.Value);
            else
                throw new HttpRequestException("No Token!!");
        }

        return await base.SendAsync(request, cancellationToken);
    }
}

```

In the Program.cs file I added the following HttpClients, one for authenticated and another ones requiring no authentication. If you got an error on the AddHttpClient method you might need to add the Microsoft.Extensions.Http nuget package to your project.

```
builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = new Uri("api URI")).AddHttpMessageHandler<CustomAuthorizationMessageHandler>();
builder.Services.AddHttpClient("ServerAPI.NoAuthenticationClient", client => client.BaseAddress = new Uri("API URI"));

```

On the razor component making the api call you can use the code from the docs:

```
@inject IHttpClientFactory ClientFactory

...

@code {
    private ExampleType[] examples;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var client = ClientFactory.CreateClient("ServerAPI");

            examples = 
                await client.GetFromJsonAsync<ExampleType[]>("{API METHOD}");

            ...
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        
    }
}

```

You can also created a typed client class to handle all requests using this method, just need to declare the HttpClient in the constructor using the client factory interface.

---

<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:** [July 27, 2020, 5:05pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/77 "2020-07-27T17:05:41Z")

</div>

Hi @nathanuel81,  
Thanks for joining Auth0 Community and appreciating this tutorial, but above all, thanks for sharing this code!

---

<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:** [July 28, 2020, 8:50am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/78 "2020-07-28T08:50:28Z")

</div>

Hi @yuramag,  
Thanks for joining the Auth0 Community and appreciating my article.

Regarding point 1, you can use the [Auth0 SDK to deal with the Management API](https://auth0.github.io/auth0.net/).

For the other two points, I’m afraid not to be able to give you a satisfactory answer on my own. I will try to gather additional information from my colleagues.  
Alternatively, you can contact our [Professional Services](https://auth0.com/docs/services).

---

<div class="post-metadata">

**Author:** ![davy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/davy/32/12778_2.png) [@davy](https://community.auth0.com/u/davy)\
**Post date:** [July 30, 2020, 6:53am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/79 "2020-07-30T06:53:25Z")

</div>

Thanks @andrea.chiarelli , nice article. Looks like the default audience was the only thing I was missing.

Maybe it would be a great addition to also talk about roles & permissions.

---

<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:** [July 30, 2020, 10:22am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/80 "2020-07-30T10:22:14Z")

</div>

Thanks for that feedback @davy!

---

<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:** [July 30, 2020, 12:32pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/81 "2020-07-30T12:32:25Z")

</div>

Thanks, @davy. I will take into account your suggestion.

[Previous page](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611.md?page=3)

[Next page](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611.md?page=5)
