0

Im trying to get Users Email with Graph AD in xamarin forms, so after i login using Azure b2c and get the token i make the http request with get method using this https://graph.windows.net/me?api-version=1.6 but i didnt get the email i trying to get, here is my full code for that

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 DesignKGVC
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class GetUsersPage : ContentPage
    {
        public GetUsersPage()
        {
            InitializeComponent();
        }
        protected override async void OnAppearing()
        {
            // let's see if we have a user in our belly already
            try
            {
                var result = await App.AuthenticationClient.AcquireTokenSilentAsync(Constants.Scopes);
                GetUsersInfo(result.Token);
                await DisplayAlert("OK", "OK", "OK");
            }
            catch
            {
                // doesn't matter, we go in interactive more

            }
        }

        private async void btnlogi(object sender, EventArgs e)
        {
            try
            {

                var result = await App.AuthenticationClient.AcquireTokenAsync(
                Constants.Scopes,
                string.Empty,
                UiOptions.SelectAccount,
                string.Empty,
                null,
                Constants.Authority,
                Constants.SignUpSignInPolicy);
                GetUsersInfo(result.Token);


                // await DisplayAlert("OK", "OK", "OK");

            }
            catch (Exception)
            {
                //
            }


        }

        public async void GetUsersInfo(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);
            //new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.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();


                // just in case
                // btnSignInSignOut.Text = "Sign out";


            }
            else
            {
                lbldisplayname.Text = "Api Call Dont Work";
                //DisplayAlert("Something went wrong with the API call", responseString, "Dismiss");
            }
        }

    }
}

and instead of geting display name and email, im geting "Api Call Dont Work" that i wrote in else condition so that i assume my Http Request is not succed, so what make that happen ? I tough there is no thing that i miss and i'm already get token that i send as parameter , or maybe b2c dont support Graph Api ? EDIT this is what i get when im using App Registration in Active Directory enter image description here

Theodorus Agum Gumilang
  • 1,414
  • 4
  • 23
  • 46

2 Answers2

1

To get the signed-in user's email address, you can select it as an Application Claim in the policy:

application claims on policy edit screen

email claim

Decoded id token via jwt.ms

decoded id token

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
  • that was i looking for , is this script work https://github.com/halkar/xamarin-forms-oauth/blob/master/Xamarin.Forms.OAuth/JsonWebTokenConvert.cs ? and what nuget do you use im trying to install this nuget System.IdentityModel.Tokens.Jwt but its said it cant install in pcl profile=7 im already trying another pcl profile but still doesnt work – Theodorus Agum Gumilang Oct 31 '17 at 02:05
  • @TheodorusAgumGumilang that sounds like another question. Please start another SO post. – spottedmahn Oct 31 '17 at 14:22
0

See this guide to query the B2C tenant that contains all of the users: Azure AD B2C: Use the Azure AD Graph API

  1. Create App Registration and Key
  2. Connect to AD Graph Api w/ the key

See this SO Post for Azure AD Graph API vs Micrsoft Graph API

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
  • but what i want is, to get the user from b2c portal, if i create app registeration and key and use it , isnt that make me not use b2c and using Azure Ad instead as a login ? – Theodorus Agum Gumilang Oct 30 '17 at 03:19
  • Using the Azure AD Graph will allow you to query all users that are in the B2C Tenant i.e. all users in the B2C portal. No, you will still be using B2C. – spottedmahn Oct 30 '17 at 14:19
  • bro i already tried using app registration but just like i said its using another portal to login, instead of login via kgvc aka my b2c name, its login via Graph Api (My App Registration in Active Directory) and thats make my users cant login there. any sugestion ? – Theodorus Agum Gumilang Oct 31 '17 at 03:32