我的 Xamarin 应用程序 (PCL) 调用 Web API,如下面的代码所示:
AuthenticationResult ar = await new AuthHelper().AcquireTokenSilentAsync();
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(Settings.ApiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ar.Token);
using (HttpResponseMessage response = await client.GetAsync("api/job"))
{
response.EnsureSuccessStatusCode();
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
return result != null ? JsonConvert.DeserializeObject<ObservableCollection<JobTask>>(result) : null;
}
}
}
我的 Web API 授权如下:
[Authorize(Roles = "Admin,BusinessAdmin")]
我正在使用 Azure AD B2C 来获取令牌。我能够从 Azure AD Graph 获取用户角色。我不确定的是如何将从 Graph 查询返回的角色添加到AuthenticationResult.Token
传递给 Web API 的角色中。