Azure ad b2c Can';t使用Microsoft.Graph检索显示名称、用户名和所有其他属性

Azure ad b2c Can';t使用Microsoft.Graph检索显示名称、用户名和所有其他属性,azure-ad-b2c,blazor-server-side,Azure Ad B2c,Blazor Server Side,我有一个Blazor应用程序(可能是任何web应用程序),它使用Microsoft.Graph从AD B2C检索用户数据。但是检索到的用户的所有属性都为null。我已授予User.Read.All权限。这是我的密码: IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(_adB2CSetting

我有一个Blazor应用程序(可能是任何web应用程序),它使用Microsoft.Graph从AD B2C检索用户数据。但是检索到的用户的所有属性都为null。我已授予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();

但我没帮上忙。我缺少什么?

如果要检索用户的属性,可以尝试以下代码,该代码完美地将用户的属性输出到控制台:


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));

        }
    }
}
    

代码看起来不错。它甚至在用户对象中获取属性id或userPrincipalName的任何值吗?@VivekN它只有ODataType(值“microsoft.graph.user”)和AdditionalData,其中包含json和所有用户数据。但我无法判断其中哪一个是我感兴趣的。请确保使用beta endpoint,并使用graph explorer网站获取正确的属性名,您使用的某些属性名肯定是空的。@Jas Suri-MSFT抱歉,我不清楚。正如我所提到的,首先我尝试不使用Select,但即使是displayname这样的基本属性也是空的。使用beta端点意味着什么?我对所有这些都不是很陌生。在深入到SDK之前,请尝试使用,并使用端点来实现。一旦您看到这里返回的原始属性,那么您就可以找出哪些SDK方法是有意义的,现在您是在黑暗中钓鱼。从技术上讲,B2C用户不必具有您“选择”的任何属性。使用图形浏览器首先确认用户是否拥有它们