TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Theodorus Gumilang
NA
95
894
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
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");
}
}
}
}
Reply
Answers (
0
)
how to solve An exception of type 'System.Xml.XmlException'
What am I Doing Wrong?