Blazor hybrid (MAUI) authentication with Blazor web view
Asked Answered
B

1

6

I'm attempting to build a small sample Blazor Maui app which authenticates the user with identity server. I'm trying to get to the point where I can login from the Maui app and then, whenever I use the blazorWebview component, the authentication state of the user can be accessed for Auth decisions.

At the moment I have the identity server login flow setup in a razor component in the Maui app, which completes the login flow.

Now I want to be able to pass that login state / principal information to the Blazor web view to use in components (some of which are referenced in an external class library). Has anyone had luck with doing this?

(Ps. I have followed the Microsoft Docs here to implement my own Custom Authentication State Provider, and added it to dependency injection but there is no instructions on how this would carry authentication information across to the Blazor web view).

Beaconsfield answered 1/7, 2022 at 16:8 Comment(9)
Googling Blazor hybrid (MAUI) authentication with Blazor web view, I see one maybe related Q&A: https://mcmap.net/q/1920159/-net-maui-blazor-hybrid-msal-authentication/199364Swivel
Clarification, are you using a maui app with a webview component or are you using a Blazor Maui app? Basically do you have a file called Main.razor or not? If not i think you have to inject the AuthStateProvider in those razor components and get the User from there. Maybe to a base component and make those ones inherit from it. If this is ok for you i can write an answer with more details.Marigraph
How did you get the 'identity server login flow' working? That's what I'm stuck with #72641863 - here's a repo that should help with your question github.com/mitchelsellers/…Sizzler
Thanks all for the responses, I ended up solving the issue but will address the comments here. I was using the Blazor Maui app with a main.razor, this is the component the BlazorWebView component pointed to in MainPage.Xaml.Beaconsfield
Jimmy, identity server side and client side have to be set up correctly (I found this annoying to setup at first). Ensuring that the allowed redirect uri/post logout redirect uri is set on the identity server side and client side by using the reverse DNS app names ( e.g. Idserver ( com.mycompany.myapp), Maui (com.mycompany.myapp://callback). (assuming you are using the oidcclient in the IdentityModel Nuget package), the flow will be able to complete properly.Beaconsfield
You also might have to add a new activity for web authentication that extends WebAuthenticatorCallbackActivity and put an intent filter on it. And add Android.support.customtabs.action Customtabsservice action under the intents section of the Android manifest.Beaconsfield
You can post your comment as an answer. This is helpful to others who are facing the same problem.Sidestep
" I ended up solving the issue" - HOW?Vengeance
Does this answer your question? .net maui blazor hybrid MSAL authenticationGlobate
W
-1

Here's a repo that should help with your question.

This sample shows how to use the IdentityModel.OidcClient FOSS library to connect a .NET MAUI Blazor app to IdentityServer

start browser-based authentication flows, which listen for a callback to a specific URL registered to the app.

Watchword answered 30/1, 2023 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.