Looking for working/good example of Azure MobileClient Services integrated with Azure B2C

I am looking for a good example of the todo list application or zumoapp or any app that shows how to use Azure MobileServices with Azure B2C Authentication/Authroization. I can get logged into my Azure b2c tenant but I can't get it to return an access_token that I can then use to pass into mobile services.

I setup the authicationclient in the app.xaml.cs file and then the app has the user navigate to settings and click a logon button to authenticate with AzureB2C. I can't seem to figure out how to get the access_token to return a value. I seem to be getting authenticated from Azure B2C but not getting the access_token.

I am wondering if it has to do with the scopes I am using when I call acquiretokeninteractive but I am having a hard time figuring out what that might be and look like.

A working example would be awesome.

Thanks

App.xaml.cs

                    public static IPublicClientApplication AuthenticationClient { get; private set; }
                    public static object UIParent { get; set; } = null;
                    public App()
                    {
                        InitializeComponent();
                        AuthenticationClient = PublicClientApplicationBuilder.Create(Constants.ClientId)
                            .WithIosKeychainSecurityGroup(Constants.IosKeychainSecurityGroups)
                            .WithB2CAuthority(Constants.AuthoritySignin)
                            //.WithRedirectUri($"msal{Constants.ClientId }://auth")
                            .Build();
                        MainPage = new AppShell();
                    }

Settings.xaml.cs

               async void OnLoginButtonClicked(object sender, EventArgs e)
                {
                    AuthenticationResult result;
                    IEnumerable<IAccount> accounts;

                    try
                    {
                        System.Diagnostics.Debug.Write("before await get tokken");
                    result = await App.AuthenticationClient
                    .AcquireTokenInteractive(Constants.Scopes)
                    .WithPrompt(Prompt.SelectAccount)
                    .WithParentActivityOrWindow(App.UIParent)

                    .WithUseEmbeddedWebView(true) // this line was not in the sample, had to add to get to work on android without chrome installed
                    .ExecuteAsync();


                JObject objToken = new JObject();
                objToken.Add("access_token", result.AccessToken);

                System.Diagnostics.Debug.WriteLine("access_token:", objToken.ToString());

// Access_token at this point is null, idToken has a value.

            MobileServiceUser user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory , objToken);


            accounts = await App.AuthenticationClient.GetAccountsAsync();
            //var account = GetUserData(result.AccessToken);
            //await Navigation.PushAsync(new LogoutPage(result));
        }
        catch (MsalException ex)
        {
            if (ex.Message != null && ex.Message.Contains("AADB2C90118"))
            {
                //result = await OnForgotPassword();
                //await Navigation.PushAsync(new LogoutPage(result));
            }
                else if (ex.ErrorCode != "authentication_canceled")
                {
                        await DisplayAlert("An error has occurred", "Exception message: " + ex.Message, "Dismiss");
                    }
                }
            }

Constants.cs

        public static class Constants
        {
            // set to a unique value for your app, such as your bundle identifier. Used on iOS to share keychain access.
            static readonly string iosKeychainSecurityGroup = "com.xxxnextbook.mynextbook";
            public static string ApplicationURL = @"https://xxxnextbookwebapp.azurewebsites.net";
            static readonly string tenantName = "xxxNextBook";
            static readonly string tenantId = "xxxNextbook.onmicrosoft.com";
            static readonly string clientId = "xxxxxxxx-72df-45e3-a84b-717317ec75c3";
            static readonly string policySignin = "B2C_1_xxxNextBookSignIn";
            static readonly string policyPassword = "B2C_1_xxxNextBookPasswordReset";

            // The following fields and properties should not need to be changed
            static readonly string[] scopes = { "https://xxxxNextbook.onmicrosoft.com/xxxNextBook/read" };
            static readonly string authorityBase = $"https://{tenantName}.b2clogin.com/tfp/{tenantId}/";
....
}
Sign In or Register to comment.