The code provided in this article is for .NET 8.0. It has not been tested with .NET 9.0
Nevermind, I was missing AddCascadingAuthenticationState()
.
Cool! Thank you for sharing
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
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)
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
@charles4,
FYI, our MAUI SDK now has support for .NET 9.0 that works around the WebAuthenticator issue. Here are more info.
Thanks for providing this resource. It was pretty helpful in adding Auth0 to my Blazor Hybrid app. I did encounter a few issues when following this article.
First, using myapp://callback
in the Auth0 config and using myapp://callback/
in the C# code, as the article said to do, resulted in a mismatch that resulted in an error message on the login page. Changing the URI to myapp://callback/
in the Auth0 config (so that the config and the C# code matched) fixed this issue.
Second, I was getting an error when logging out, but then I saw that there’s an open issue on the example project that describes how to fix the error. It’d be nice if the article and the example project was updated to include this fix. I’ve gone ahead and created a pull request for the example project to hopefully make it quicker and easier for you.
Third, following this article results in the user being logged out every time they close the app. I would think that for most people, this wouldn’t be the desired functionality. It’d be nice if you linked your Managing Tokens in .NET MAUI article within this article instead of just in the comments.
Besides those minor issues, this article was really well written and easy to follow. Thanks for the effort you’ve put into this article. It saved me a lot of time.
Hey @Jon-Corey,
Thank you very much for your suggestions They totally make sense, and I applied all three.
The article has already been updated, and your PR will be merged soon.
Thank you again for your active contribution!