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 isnt 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 ClaimsPrinciple and that is populated when the app is first loaded up, Ive used some DI to to set the scoped AuthenticatedUser but its not attaching its self to the Authentication StateProvider
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 here
var result = await authService.LoginAsync(CancellationToken.None);
var token = result?.IdToken; // you can also get AccessToken if you need it
if (token != null)
{
var handler = new JwtSecurityTokenHandler();
var data = handler.ReadJwtToken(token);
var claims = data.Claims.ToList();
}
_authenticatedUser.Principal = result.ClaimsPrincipal;
Auth Service 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("[TENNANT 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 _authencatedUser.Principle to this claim,
In 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 im asking is how to i attach this Stateprovider to the Maui Blazor and then use Authorization view to get the context identity