I am making a call to Azure B2C that appears to be successful. I am getting an id_token but not an access_token that I can use to a latter call to api's and mobileservices.
I am writing a mobile app using xamarin with the microsoft.identity.client package. Some help in pointing me in the right direction for getting an access_token returned would be appreciated.
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(); }
Page that displays a button that calls loginasync
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}/"; .... }
Brady Brady@acm.org