How to Using Graph AD For Azure B2C in Xamarin Forms ?

Oct 27 2017 12:12 AM
Im Trying to get email in b2c with Graph AD in xamarin forms , because authentication result in microsoft.identity.client dont provide that . So i try this code that i make but i didnt get the display name and email that i want to get. Im able to login and get the Token but the label its not change. what is wrong in here ? i tought my code will work
 here is my code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Http;  
  5. using System.Net.Http.Headers;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.Identity.Client;  
  9. using Newtonsoft.Json.Linq;  
  10. using Xamarin.Forms;  
  11. using Xamarin.Forms.Xaml;  
  12.   
  13. namespace GetUserProfile  
  14. {  
  15. [XamlCompilation(XamlCompilationOptions.Compile)]  
  16. public partial class GetUsersPage : ContentPage  
  17. {  
  18. public GetUsersPage()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22. protected override async void OnAppearing()  
  23. {  
  24. // let's see if we have a user in our belly already  
  25. try  
  26. {  
  27. var result = await App.AuthenticationClient.AcquireTokenSilentAsync(Constants.Scopes);  
  28. UsersInfo(result.Token);  
  29. await DisplayAlert("OK""OK""OK");  
  30.   
  31. // await DisplayAlert("OK", "OK", "OK");  
  32.   
  33. }  
  34. catch  
  35. {  
  36. // doesn't matter, we go in interactive more  
  37.   
  38. }  
  39. }  
  40.   
  41.   
  42. private async void GetUserInfo(object sender, EventArgs e)  
  43. {  
  44. try  
  45. {  
  46.   
  47. var result = await App.AuthenticationClient.AcquireTokenAsync(  
  48. Constants.Scopes,  
  49. string.Empty,  
  50. UiOptions.SelectAccount,  
  51. string.Empty,  
  52. null,  
  53. Constants.Authority,  
  54. Constants.SignUpSignInPolicy);  
  55. UsersInfo(result.Token);  
  56. // RefreshUserData(result.Token);  
  57. //btnSignInSignOut.Text = "Sign out";  
  58. await DisplayAlert("OK""OK""OK");  
  59.   
  60. }  
  61. catch (Exception)  
  62. {  
  63. //  
  64. }  
  65.   
  66.   
  67. }  
  68.   
  69. public async void UsersInfo(string token)  
  70. {  
  71. var client = new HttpClient();  
  72. var request = new HttpRequestMessage(HttpMethod.Get,  
  73. "https://graph.windows.net/me?api-version=1.6");  
  74. request.Headers.Authorization =  
  75. new AuthenticationHeaderValue("Bearer", token);  
  76. var response = await client.SendAsync(request);  
  77. var content = await response.Content.ReadAsStringAsync();  
  78. if (response.IsSuccessStatusCode)  
  79. {  
  80. JObject user = JObject.Parse(content);  
  81.   
  82.   
  83. lbldisplayname.Text = user["displayName"].ToString();  
  84. lblmail.Text = user["otherMails"].ToString();  
  85.   
  86.   
  87. // just in case  
  88. // btnSignInSignOut.Text = "Sign out";  
  89.   
  90.   
  91. }  
  92. else  
  93. {  
  94. lbldisplayname.Text = "Damnit Its Failed";  
  95. //DisplayAlert("Something went wrong with the API call", responseString, "Dismiss");  
  96. }  
  97. }  
  98. }  
  99. }