locked
Migrating from ADAL to MSAL - Xamarin Forms - Login Page not opening RRS feed

  • Question

  • User244957 posted

    Hi, Followed the below article to migrate the Azure AD authentication - ADAL to MSAL authentication. This approach is working till Android 9/10. When tested the same logic on Android 11, the login page is not opening. Any idea?

    https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-migration-android-broker

    This is the logic written to open the Login Page and authenticate:

    string[] scope = { resource + "/.default" };

    // clientId from Azure // RedirectUri generated from Azure. IPublicClientApplication clientApplication = PublicClientApplicationBuilder.Create(clientId) .WithAuthority("https://login.microsoftonline.com/common") .WithRedirectUri("msauth://<>/T1f%2BFe....")
    .WithBroker() .Build();

            IEnumerable<IAccount> accounts = await clientApplication.GetAccountsAsync();
            IAccount firstAccount = accounts.FirstOrDefault();
            AuthenticationResult result = null;
            try
            {
                result = await clientApplication.AcquireTokenSilent(scope, firstAccount).ExecuteAsync();
                return result;
            }
            catch(MsalUiRequiredException ex)
            {
                // A MsalUiRequiredException happened on AcquireTokenSilent. 
                // This indicates you need to call AcquireTokenInteractive to acquire a token
                try
                {               
                      result = await clientApplication.AcquireTokenInteractive(scope)
                        .WithParentActivityOrWindow((Activity)Forms.Context)
                        .WithUseEmbeddedWebView(true)
                        .ExecuteAsync();
                    return result;
                }
                catch(MsalException msalex)
                {                   
                    return null;
                }
                catch(Exception exc)
                {
                    return null;
                }
            }
            catch(WebException wex)
            {
                return null;
            }
            catch(Exception ex)
            {
                return null;
            }
    
    Saturday, January 23, 2021 9:28 AM

All replies

  • User369978 posted

    Did you follow the general tips for troubleshooting ?

    • Update the existing MSAL.NET NuGet package to the latest version of MSAL.NET.
    • Verify that Xamarin.Forms is on the latest version.
    • Verify that Xamarin.Android.Support.v4 is on the latest version.
    • Ensure all the Xamarin.Android.Support packages target the latest version.
    • Clean or rebuild the application.
    • In Visual Studio, try setting the maximum number of parallel project builds to 1. To do that, select Options > Projects and Solutions > Build and Run > Maximum number of parallel projects builds.
    • If you're building from the command line and your command uses /m, try removing this element from the command.

    Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.

    Monday, January 25, 2021 7:32 AM