October 22, 2024
Chicago 12, Melborne City, USA
Android

On Android, you have to specify the current Activity from which the browser pop-up will be displayed using the WithParentActivityOrWindow method


I’m implementing Azure AD B2C authentication in my .NET MAUI app using MSAL.NET. My .NET MAUI app targets .NET 8 and I’m using Microsoft.Identity.Client.Extensions.Msal version 4.66.0.

My app uses the AppShell and upon start, it hits the InitializeAsync() method of my StartupPage.cs. The first thing I do in InitializeAsync() is to make the following call:

var authenticatedUser = await _authService.IsAuthenticated();. This method looks like this:

public async Task<User> IsAuthenticated()
{
   await PublicClientSingleton.Instance.AcquireTokenSilentAsync();
   var claims = PublicClientSingleton.Instance.MSALClientHelper.AuthResult.ClaimsPrincipal.Claims; // This throws that error!

   ...
}

And this is where the exception is thrown.

BTW, this is what my MainActivity.cs file looks like under Android in Platforms folder:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    // configure platform specific params
    PlatformConfig.Instance.RedirectUri = $"msal{PublicClientSingleton.Instance.MSALClientHelper.AzureADB2CConfig.ClientId}://auth";
    PlatformConfig.Instance.ParentWindow = this;

    // Initialize MSAL and platformConfig is set
    _ = Task.Run(async () => await PublicClientSingleton.Instance.MSALClientHelper.InitializePublicClientAppAsync()).Result;
}

Any suggestions on how I can address this issue?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video