Add Auth0 Authentication to Blazor Hybrid Apps in .NET MAUI

The code provided in this article is for .NET 8.0. It has not been tested with .NET 9.0

1 Like

Nevermind, I was missing AddCascadingAuthenticationState().

Cool! Thank you for sharing :slightly_smiling_face:
So this means it’s working in .NET 9 as well, right?

Yes it does as long as you don’t forget the cascading authentication state :slight_smile:

1 Like

Hello Andrea, thanks for the article.

I only have one question related to the Blazor Web project. My Maui solution has 3 projects, the mobile the shared project and the web project (Blazor WASM). Currently only the Mobile works, but when I run the web I am getting error:

rit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Could not load type of field ‘Auth0.OidcClient.WebAuthenticatorBrowser+d__0:<>u__1’ (3) due to: Could not load file or assembly ‘Microsoft.Maui.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies.
System.TypeLoadException: Could not load type of field ‘Auth0.OidcClient.WebAuthenticatorBrowser+d__0:<>u__1’ (3) due to: Could not load file or assembly 'Microsoft.Maui.Essentials, Version=1.0.0.0

In order to make the authentication to work for the web project on Blazor WebAssembly should I do the implemention with the traditional approach for Blazor WASM ? or there is a way to use the same logic for all ?

Thanks

Hey @dsapo,
You can’t use the MAUI SDK in a Blazor WASM app.
A Blazor WASM app is still a web app, not a native app. You should use the approaches described here or here (it depends on the .NET version and/or the hosting model)

1 Like

Could you elaborate I keep throwing an error when I click the login link in .NET 9 MAUI on Windows, IOS and Android.

This implementation of WebAuthenticator does not support Windows. See WebAuthenticationBroker API that supports all WinAppSDK OS's and application types · Issue #441 · microsoft/WindowsAppSDK · GitHub for more details.
at Microsoft.Maui.Authentication.WebAuthenticatorImplementation.AuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions)
at Microsoft.Maui.Authentication.WebAuthenticatorExtensions.AuthenticateAsync(IWebAuthenticator webAuthenticator, Uri url, Uri callbackUrl)
at Auth0.OidcClient.WebAuthenticatorBrowser.InvokeAsync(BrowserOptions options, CancellationToken cancellationToken)
at IdentityModel.OidcClient.AuthorizeClient.AuthorizeAsync(AuthorizeRequest request, CancellationToken cancellationToken) in //src/OidcClient/AuthorizeClient.cs:line 56
at IdentityModel.OidcClient.OidcClient.LoginAsync(LoginRequest request, CancellationToken cancellationToken) in /
/src/OidcClient/OidcClient.cs:line 75
at Auth0.OidcClient.Auth0ClientBase.LoginAsync(Object extraParameters, CancellationToken cancellationToken)
at Auth0AuthenticationStateProvider.LoginWithAuth0Async() in D:\repos\AuthBlazorMauiAuthO\MauiBlazorHybrid\Auth0AuthenticationStateProvider.cs:line 37
at Auth0AuthenticationStateProvider.g__LogInAsyncCore|4_0() in D:\repos\AuthBlazorMauiAuthO\MauiBlazorHybrid\Auth0AuthenticationStateProvider.cs:line 27
at MauiBlazorHybrid.Components.LoginButton.Login() in D:\repos\AuthBlazorMauiAuthO\MauiBlazorHybrid\Components\LoginButton.razor:line 17
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
NotifyUnhandledException @ blazor.webview.js:1

Hey @charles4,
Unfortunately this is a problem on the .NET side, which does not support WebAuthenticator on Windows. We are currently using a workaround, but it looks like .NET 9 broke it, and many .NET devs are experiencing this issue as well (see here).
Our team is investigating if a new workaround is available while waiting for native support in .NET.
Thanks for your patience :pray: