不要从 Xamarin Forms 中的 Graph AD 获取结果
Dont get the result from Graph AD in Xamarin Forms
我正在尝试使用 xamarin 表单中的 Graph AD 获取用户电子邮件,所以在我使用 Azure b2c 登录并获取令牌后,我使用此 https://graph.windows.net/me?api-version=1.6 通过 get 方法发出了 http 请求,但我没有收到电子邮件我正在尝试获取,这是我的完整代码
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");
}
}
}
}
而不是获取显示名称和电子邮件,我得到的是我在 else 条件下编写的 "Api Call Dont Work",因此我假设我的 Http 请求没有成功,那么是什么导致了这种情况?我很难,我没有错过任何东西,我已经得到了我作为参数发送的令牌,或者也许 b2c 不支持 Graph Api?
编辑
这是我在 Active Directory 中使用应用程序注册时得到的
请参阅本指南查询包含所有用户的 B2C 租户:Azure AD B2C: Use the Azure AD Graph API
- Create App Registration and Key
- 使用密钥连接到 AD Graph Api
Azure AD Graph API 与 Microsoft Graph API
要获取登录用户的电子邮件地址,您可以select将其作为策略中的Application Claim
:
通过jwt.ms
解码id token
我正在尝试使用 xamarin 表单中的 Graph AD 获取用户电子邮件,所以在我使用 Azure b2c 登录并获取令牌后,我使用此 https://graph.windows.net/me?api-version=1.6 通过 get 方法发出了 http 请求,但我没有收到电子邮件我正在尝试获取,这是我的完整代码
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");
}
}
}
}
而不是获取显示名称和电子邮件,我得到的是我在 else 条件下编写的 "Api Call Dont Work",因此我假设我的 Http 请求没有成功,那么是什么导致了这种情况?我很难,我没有错过任何东西,我已经得到了我作为参数发送的令牌,或者也许 b2c 不支持 Graph Api?
编辑
这是我在 Active Directory 中使用应用程序注册时得到的
请参阅本指南查询包含所有用户的 B2C 租户:Azure AD B2C: Use the Azure AD Graph API
- Create App Registration and Key
- 使用密钥连接到 AD Graph Api
要获取登录用户的电子邮件地址,您可以select将其作为策略中的Application Claim
:
通过jwt.ms
解码id token