Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

.net maui blazor hybrid MSAL authentication

$
0
0

I am trying to get authentication working though .NET maui blazor for Android, I have set up the manifests to correctly bounce off the AAD and i can get logged in and get my id from azure, the issue is the token isn't working with the Blazor authorisation.

I have followed the solution on this issue on github https://github.com/dotnet/maui/issues/2529 and placed my own hybrid authentication state provider, I have a class authenticated user which holds a ClaimsPrincipal and that is populated when the app is first loaded up, I've used some DI to to set the scoped AuthenticatedUser but its not attaching its self to the authentication state provider

Here is my code so far - this is fired when the app first starts up:

var authService = new AuthService(); // most likely you will inject it in constructor, but for simplicity let's initialize it herevar result = await authService.LoginAsync(CancellationToken.None);var token = result?.IdToken; // you can also get AccessToken if you need itif (token != null){    var handler = new JwtSecurityTokenHandler();    var data = handler.ReadJwtToken(token);    var claims = data.Claims.ToList();}_authenticatedUser.Principal = result.ClaimsPrincipal;

AuthService is:

private readonly IPublicClientApplication authenticationClient;public AuthService(){    authenticationClient = PublicClientApplicationBuilder.Create(Constants.ClientId)        //.WithB2CAuthority(Constants.AuthoritySignIn) // uncomment to support B2C        .WithRedirectUri($"msal{Constants.ClientId}://auth")        .Build();}public async Task<AuthenticationResult> LoginAsync(CancellationToken cancellationToken){    AuthenticationResult result;    try    {        result = await authenticationClient                .AcquireTokenInteractive(Constants.Scopes)                .WithAuthority("[TENANT ID HERE]")                .WithPrompt(Prompt.ForceLogin)#if ANDROID                .WithParentActivityOrWindow(Platform.CurrentActivity)#endif                .ExecuteAsync(cancellationToken);        return result;    }    catch (MsalClientException)    {        return null;    }}

And constants just holds the Client id.

So the app starts, it redirects to sign in, gets the token and gets a JWT and claims, then sets _authenticatedUser.Principal to this claim.

My HybridStateAuthenticator looks like this:

public class HybridAuthenticationStateProvider : AuthenticationStateProvider{    private readonly Task<AuthenticationState> _authenticationState;    public HybridAuthenticationStateProvider(AuthenticatedUser user) =>    _authenticationState = Task.FromResult(new AuthenticationState(user.Principal));    public override Task<AuthenticationState> GetAuthenticationStateAsync() =>    _authenticationState;}public class AuthenticatedUser{    public ClaimsPrincipal Principal { get; set; }}

What I'm asking is how do I attach this Stateprovider to the Maui Blazor and then use authorization view to get the context identity


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>