question

AmjadKhan-3554 avatar image
0 Votes"
AmjadKhan-3554 asked WenyanZhang-MSFT commented

Migrating Blazor wasm with authentication to MAUI

I have a standard Blazor wasm hosted app (counter app made by Visual Studio template) with individual accounts for authentication. If I would like to move this app into MAUI so I can use the same app as native windows app, do I need to make any changes to the Blazor authentication? Will the MAUI app able to redirect to login page running on the server side? My goal is to simply run the existing Blazor app with authentication, as a Windows app to cut down any loading time required for standard Blazor wasm websites (Loading...).

dotnet-aspnet-core-blazordotnet-maui
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

Hello @AmjadKhan-3554 ,

Authentication in Blazor Hybrid apps is handled by native platform libraries (iOS/Android/Windows/Mac and so on), which is different from Blazor WASM.
Furthermore, .NET MAUI apps use Xamarin.Essentials: Web Authenticator: The WebAuthenticator class allows the app to initiate browser-based authentication flows that listen for a callback to a specific URL registered with the app. For more details, you can refer to ASP.NET Core Blazor Hybrid authentication and authorization | Microsoft Learn
(Note: Xamarin.Essentials is a part of MAUI currently)

From your description, you want to implement a MAUI app on Windows platform, you can check the Web authenticator in MAUI. At the moment, WebAuthenticator isn't working on Windows, and you can follow the progress at https://github.com/microsoft/WindowsAppSDK/issues/441

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So my question is that almost all pages/components in my Blazor app uses authentication, Authorize attribute, getting login state/user information etc. So do I need to make any changes in those components regarding authentication? If that is the case, my Blazor code will be different from my MAUI code? For example, if a page uses Authorize attribute with a role, when I move it into the MAUI app, does anything change?

0 Votes 0 ·

So do I need to make any changes in those components regarding authentication? If that is the case, my Blazor code will be different from my MAUI code?

Yes, you should do some adjust and create a custom AuthenticationStateProvider in MAUI (outside of the BlazorWebview). And you may meet the second option- Handle authentication within the BlazorWebView (within the BlazorWebView), you can follow the code snippets to log in a user.

For example, if a page uses Authorize attribute with a role, when I move it into the MAUI app, does anything change?

If you configure your authorization as the doc described, you can use Authorize attribute as well, see the closed issue at GitHub




0 Votes 0 ·