azure-ad-b2c - 无法使用 Microsoft.Graph 检索显示名称、用户名和所有其他属性
问题描述
我有一个 Blazor 应用程序(可能是任何 Web 应用程序),它使用 Microsoft.Graph 从 AD B2C 检索用户数据。但是检索到的用户的所有属性都是空的。我已授予 User.Read.All 权限。这是我的代码:
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(_adB2CSettings.ClientId)
.WithTenantId(_adB2CSettings.TenantId)
.WithClientSecret(_adB2CSettings.ClientSecret)
.Build();
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
return await graphServiceClient.Users[userId].Request().GetAsync();
我什至试过
return await graphServiceClient.Users[userId].Request().Select("displayName,mail,country,contacts,contactFolders").GetAsync();
但是id没有帮助。我错过了什么?
解决方案
如果要检索用户的属性,可以试试下面的代码,完美地将用户的属性输出到控制台:
using System;
using Microsoft.Identity.Client;
using Microsoft.Graph.Auth;
using Microsoft.Graph;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace call_api
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create("{client id}")
.WithClientSecret("{Client Secret}")
.WithAuthority(new Uri("https://login.microsoftonline.com/{B2C tenant id}"))
.Build();
AuthenticationResult result = null;
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
string accesstoken = result.AccessToken;
/*Console.WriteLine(accesstoken);*/
ClientCredentialProvider authProvider = new ClientCredentialProvider(app);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var user = await graphClient.Users["{user id}"].Request().GetAsync();
Console.WriteLine("user properties:" + JsonConvert.SerializeObject(user));
}
}
}
推荐阅读
- dart - 如何使用 url 中的参数发出颤振发布请求,如获取请求
- python - Masonite:生产数据的迁移
- ruby - 提取非连续集
- php - PhpStorm & Symfony - 无法让 PhpStorm 识别某些服务
- encryption - 当 yml 嵌入加密变量时 include_vars 期间出错
- javascript - 如何以块的形式发送包含多个文件的 FormData
- wolfram-mathematica - 除非我先设置微分函数,否则 Mathematica dsolve 会失败
- php - 在 css 中使用 php,substr_count() 搜索文件位置
- laravel - 满足条件时,Laravel 允许用户访问特定路线
- android - 可点击视图是异步的?