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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Identity.Client;
- using Newtonsoft.Json.Linq;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
-
- namespace GetUserProfile
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class GetUsersPage : ContentPage
- {
- public GetUsersPage()
- {
- InitializeComponent();
- }
- protected override async void OnAppearing()
- {
-
- try
- {
- var result = await App.AuthenticationClient.AcquireTokenSilentAsync(Constants.Scopes);
- UsersInfo(result.Token);
- await DisplayAlert("OK", "OK", "OK");
-
-
-
- }
- catch
- {
-
-
- }
- }
-
-
- private async void GetUserInfo(object sender, EventArgs e)
- {
- try
- {
-
- var result = await App.AuthenticationClient.AcquireTokenAsync(
- Constants.Scopes,
- string.Empty,
- UiOptions.SelectAccount,
- string.Empty,
- null,
- Constants.Authority,
- Constants.SignUpSignInPolicy);
- UsersInfo(result.Token);
-
-
- await DisplayAlert("OK", "OK", "OK");
-
- }
- catch (Exception)
- {
-
- }
-
-
- }
-
- public async void UsersInfo(string token)
- {
- var client = new HttpClient();
- var request = new HttpRequestMessage(HttpMethod.Get,
- "https://graph.windows.net/me?api-version=1.6");
- request.Headers.Authorization =
- new AuthenticationHeaderValue("Bearer", token);
- var response = await client.SendAsync(request);
- var content = await response.Content.ReadAsStringAsync();
- if (response.IsSuccessStatusCode)
- {
- JObject user = JObject.Parse(content);
-
-
- lbldisplayname.Text = user["displayName"].ToString();
- lblmail.Text = user["otherMails"].ToString();
-
-
-
-
-
-
- }
- else
- {
- lbldisplayname.Text = "Damnit Its Failed";
-
- }
- }
- }
- }