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

<div class="post-metadata">

**Author:** ![zgrose](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/zgrose/32/22210_2.png) [@zgrose](https://community.auth0.com/u/zgrose)\
**Post date:** [July 30, 2020, 6:48pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/82 "2020-07-30T18:48:21Z")

</div>

Just two notes from when I was adding these helpful code bits to my project that may help others:

- If you’re just going to connect from the wasm to the host, extend BaseAddressAuthorizationMessageHandler instead of AuthorizationMessageHandler. It sets the URL for you so you don’t need the ConfigureHandler() call.

- You need to put your MessageHandler into the services, e.g.

---

<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 31, 2020, 9:23am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/83 "2020-07-31T09:23:09Z")

</div>

Thanks for sharing that @zgrose!

---

<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 1, 2020, 6:52am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/84 "2020-08-01T06:52:36Z")

</div>

Thanks for your contribution @zgrose!

---

<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:** [August 28, 2020, 2:19am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/85 "2020-08-28T02:19:38Z")

</div>

Is it possible to redirect a user to the register page directly instead of going to the login first and then navigate to the register?

---

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

</div>

Hey @nathanuel81,  
you should be able to get this by customizing the Universal Login page.  
Basically, you should access [the Universal Login section in your Auth0 Dashboard](https://manage.auth0.com/#/login_settings) and modify the page’s code in the _Login_ tab.

Check out [this document](https://auth0.com/docs/universal-login/classic-experience/customization-classic) to have an idea of the overall process.

For your specific needs, you should work on the [`initialScreen`](https://auth0.com/docs/libraries/lock/lock-configuration#initialscreen-string-) option of the Configuration object.

---

<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:** [September 3, 2020, 1:08pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/87 "2020-09-03T13:08:05Z")

</div>

Hey Andrea,

I was more thinking along the lines of having a button to go register and another to login within my UI which would redirect the user to the relevant page of the universal login.

I done something similar in an MVC application using the following code:

```
    [AllowAnonymous]
    public async Task SignUp(string returnUrl = "/")
    {
        var properties = new AuthenticationProperties { RedirectUri = returnUrl };
        properties.Items.Add("screen_hint", "signup");
        await HttpContext.ChallengeAsync("Auth0", properties);
    }

```

Is there a way to pass this screen\_hint property in Blazor?

---

<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 9, 2020, 12:46pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/88 "2020-09-09T12:46:22Z")

</div>

Hey @nathanuel81,  
I didn’t try, but I think you can do it by handling the `OnRedirectToIdentityProvider` event (see [here](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.openidconnect.openidconnectevents.onredirecttoidentityprovider?view=aspnetcore-3.0)), similarly to how you are handling the logout redirection.  
Let me know if it works.

---

<div class="post-metadata">

**Author:** ![jens.bruggeman](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jens.bruggeman/32/13320_2.png) [@jens.bruggeman](https://community.auth0.com/u/jens.bruggeman)\
**Post date:** [October 2, 2020, 2:58pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/89 "2020-10-02T14:58:50Z")

</div>

I’ve successfully added Auth0 authentication to my existing Blazor Server application.  
Authentication (login/logout/…) & views are authorized properly. But how could I protect the back-end controllers which are also part of the application?  
Could I easily get the token from the razor pages and pass it through the controller ?  
f.e. via httpclient

---

<div class="post-metadata">

**Author:** ![zgrose](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/zgrose/32/22210_2.png) [@zgrose](https://community.auth0.com/u/zgrose)\
**Post date:** [October 2, 2020, 4:53pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/90 "2020-10-02T16:53:45Z")

</div>

That’s what the message handler is doing for you. Appending the Bearer token into the headers. Then you just need to put the JWT bearer bits in the server.

> [@zgrose](#):
>
> ```auto
> builder.Services.AddHttpClient("ServerAPI", client => 
> client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
> .AddHttpMessageHandler<Auth0MessageHandler>();
> 
> ```

---

<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 5, 2020, 10:43am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/91 "2020-10-05T10:43:19Z")

</div>

Thanks for sharing that with the rest of community @zgrose!

---

<div class="post-metadata">

**Author:** ![jens.bruggeman](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jens.bruggeman/32/13320_2.png) [@jens.bruggeman](https://community.auth0.com/u/jens.bruggeman)\
**Post date:** [October 6, 2020, 11:23am UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/92 "2020-10-06T11:23:26Z")

</div>

Thank you for your response.

In which package is “Auth0MessageHandler” defined? I don’t seem to find this directly on Github

---

<div class="post-metadata">

**Author:** ![zgrose](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/zgrose/32/22210_2.png) [@zgrose](https://community.auth0.com/u/zgrose)\
**Post date:** [October 6, 2020, 2:55pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/93 "2020-10-06T14:55:39Z")

</div>

In this thread, the post by nathanuel81.  
The official docs link is: [ASP.NET Core Blazor WebAssembly additional security scenarios | Microsoft Learn](https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-3.1)

The version I created based on nathanuel81’s addition uses a different constructor otherwise it’s basically the same thing.

-z

```
public class Auth0MessageHandler : BaseAddressAuthorizationMessageHandler
{
    private readonly IAccessTokenProvider _provider;

    public Auth0MessageHandler(IAccessTokenProvider provider,
        NavigationManager navigationManager)
        : base(provider, navigationManager)
    {
        _provider = provider;
    }

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

        if (accessTokenResult.TryGetToken(out var token))
        {
            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);
    }
}

```

---

<div class="post-metadata">

**Author:** ![jens.bruggeman](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jens.bruggeman/32/13320_2.png) [@jens.bruggeman](https://community.auth0.com/u/jens.bruggeman)\
**Post date:** [October 7, 2020, 1:18pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/94 "2020-10-07T13:18:54Z")

</div>

Thank you. I’ve created this handler however, I receive the following error

.Auth0MessageHandler’: Unable to resolve service for type ‘Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider’ while attempting to activate

Shouldn’t this Interface be resolved by the framework itself? Do I need to manually resolve this?  
Creating a Blazor Server app & using Typed HttpClient

```
        services.AddScoped<Auth0MessageHandler>();

        services.AddHttpClient<ApiService>(client =>
        {
            client.BaseAddress = new Uri("https://localhost:44364/");
        }).AddHttpMessageHandler<Auth0MessageHandler>();

```

---

<div class="post-metadata">

**Author:** ![zgrose](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/zgrose/32/22210_2.png) [@zgrose](https://community.auth0.com/u/zgrose)\
**Post date:** [October 7, 2020, 4:36pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/95 "2020-10-07T16:36:48Z")

</div>

Did you add OIDC to the services? I believe that extension is what adds the token provider.  
Something along the lines of:

```
            builder.Services.AddOidcAuthentication(options =>
        {
            builder.Configuration.Bind("Auth0", options.ProviderOptions);
            options.ProviderOptions.ResponseType = OpenIdConnectResponseType.Code;
        });

```

---

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

</div>

Let us know @jens.bruggeman if what @zgrose suggested solved your hurdle!

---

<div class="post-metadata">

**Author:** ![dan6](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan6/32/12778_2.png) [@dan6](https://community.auth0.com/u/dan6)\
**Post date:** [October 29, 2020, 7:55pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/97 "2020-10-29T19:55:13Z")

</div>

Hi,

Firstly, sorry if this a rookie question, I’ve never had to deal with authentication before.

I’m creating a Blazor Server Side application, and implemented Auth0 using the following blog post by Auth0:

> **[How to Build and Secure Web Applications with Blazor](https://auth0.com/blog/what-is-blazor-tutorial-on-building-webapp-with-authentication/#Securing-the-Application-with-Auth0)**
>
> Learn how to build client-side Web apps using Blazor and how to secure them with Auth0 authentication and authorization features.

I’ve created a user and added a property in the App\_Metadata, to let me set information for the user but stuff they can’t change (CompanyId: 3).

How do I get the CompanyId from the App\_Metadata in Auth0 into my application? It’s not a role, but gives me information about the user to link them to more data in my own database.

---

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

</div>

Hey there @dan6!

Migrating your post here as this is the thread for issues and questions regarding this blog article.

---

<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:18pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/100 "2020-11-06T14:18:00Z")

</div>

Hi @dan6,  
Welcome to Auth0 Community and thanks for reading this blog post.

Regarding your question, in general, you should find your user’s data in the `User.Identity` object. However, there are two points to point out:

1. By default, the `User.Identity` object is not automatically populated. You should apply a few changes to your code in order to map user’s data to that object. Please, take a look at [this](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/25) to get more info.

2. The user’s data that populate the `User.Identity` object comes from the ID token issued by Auth0. However, these data are just [the standard OIDC claims](https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims).  
To include additional information as part of the ID token (the `App_Metadata`, for example), you need to add custom claims through a rule.  
Please, check out these documents to learn more:

- [Metadata](https://auth0.com/docs/users/metadata)
- [Auth0 Rules](https://auth0.com/docs/rules)
- [Use Metadata in Rules](https://auth0.com/docs/rules/metadata)
- [Add custom claims to a token](https://auth0.com/docs/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token)

---

<div class="post-metadata">

**Author:** ![ehilimire](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ehilimire/32/28744_2.png) [@ehilimire](https://community.auth0.com/u/ehilimire)\
**Post date:** [April 12, 2021, 2:21pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/101 "2021-04-12T14:21:33Z")

</div>

Can we please get a working example for Blazor .Net 5, [Asp.Net](http://Asp.Net) Core Hosted WASM. This example does not work. A sample project would be appreciated.

---

<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 12, 2021, 3:23pm UTC](https://community.auth0.com/t/what-is-blazor-a-tutorial-on-building-web-apps-with-authentication/33611/102 "2021-04-12T15:23:57Z")

</div>

Hi @ehilimire,  
Welcome to the Auth0 Community!  
An update to this article is under consideration. I will announce it here when it becomes available (hopefully very soon).  
Thank you for your interest 🙏

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

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