Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
0
Reply
How to Using Graph AD For Azure B2C in Xamarin Forms ?
Theodorus Gumilang
Oct 27 2017 12:12 AM
251
Reply
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()
{
// let's see if we have a user in our belly already
try
{
var result = await App.AuthenticationClient.AcquireTokenSilentAsync(Constants.Scopes);
UsersInfo(result.Token);
await DisplayAlert(
"OK"
,
"OK"
,
"OK"
);
// await DisplayAlert("OK", "OK", "OK");
}
catch
{
// doesn't matter, we go in interactive more
}
}
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);
// RefreshUserData(result.Token);
//btnSignInSignOut.Text = "Sign out";
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();
// just in case
// btnSignInSignOut.Text = "Sign out";
}
else
{
lbldisplayname.Text =
"Damnit Its Failed"
;
//DisplayAlert("Something went wrong with the API call", responseString, "Dismiss");
}
}
}
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
how to solve An exception of type 'System.Xml.XmlException'
What am I Doing Wrong?