Callback URL scheme not working with WebAuthenticator for Xamarin app with Azure portal reg.

Anish Kamble 0 Reputation points
2024-01-11T08:52:17.27+00:00

Hi, I am trying to authenticate my Xamarin app with Azure authentication. I am using Xamarin Essentials WebAuthenticator class for this. I have registered my app on Azure, and also added iOS platform

			var url = new Uri("https://login.microsoftonline.com");
            var callbackUrl = new Uri("msauth.com.sampleapp://auth");
            var res = await WebAuthenticator.AuthenticateAsync(new WebAuthenticatorOptions
            {
                Url = url,
                CallbackUrl = callbackUrl,
                PrefersEphemeralWebBrowserSession = true
            });

            var accessToken = res?.AccessToken;


In the above code, "msauth.com.sampleapp://auth" is the callback URL which is also added in the iOS platform of the Azure app registration. In Info.Plist, I added "msauth.com.sampleapp" in the URLscheme. When i run the app, The azure authentication login page is displayed, and I am able to successfully sign in. But after that, the callback URL is never triggered, and I do not get the access token in the app. Am i missing anything? Is the Callback scheme correct? I am guessing there is something to do with incorrect callback url. I followed the steps given in https://learn.microsoft.com/en-us/xamarin/essentials/web-authenticator?tabs=ios Please let me know. Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,879 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,666 Reputation points Microsoft Employee
    2024-01-18T17:49:38.1333333+00:00

    Hi @Anish Kamble

    I personally worked on a Xamarin/.NET MAUI project couple of years ago. For me, I couldn't get the WebAuthenticator class to work for me either. What did work though was using MSAL.NET library and the PublicClientApplication class. You can follow Migrate Xamarin apps using brokers to MSAL.NET - Microsoft identity platform | Microsoft Learn on switching. As for acquiring tokens, I did what's in the Acquiring tokens interactively - Microsoft Authentication Library for .NET | Microsoft Learn guide. I did want to the WebAuthenticator to work due to its simplicity but going the MSAL.net route was more successful.

    0 comments No comments