Azure AD B2C和图表:无法列出具有memberOf

时间:2018-12-04 16:47:17

标签: c# .net azure azure-ad-b2c azure-ad-graph-api

我正在尝试列出B2C租户中的用户以及他们所属的组。我正在以以下应用程序模式连接到Graph:

var clientCred = new ClientCredential("<client id>",  "<secret>");
var authContext = new AuthenticationContext("https://login.windows.net/" + "<b2c tenant>");
var authResult = authContext.AcquireTokenAsync("https://graph.microsoft.com/", clientCred).Result;
var client = new GraphServiceClient(
    new DelegateAuthenticationProvider(
        async (requestMessage) =>
        {
            var token = authResult.AccessToken;
            var result = await Task.FromResult(token);

            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
        }));

var users = client
    .Users
    .Request()
    .Select("memberOf")  // not working
    //.Select(u => new { u.MemberOf }) // not working
    .Expand("memberOf") // not working
    //.Expand(u => new { u.MemberOf }) // not working
    .GetAsync()
    .Result
    .ToList();

结果是我得到了一些数据的用户列表,但是MemberOf字段始终为空(尽管我100%确信他们至少有一个分配的组)。然而,这按预期工作:

var u = client.Users["<id>"].MemberOf.Request().GetAsync().Result;

可以肯定的是,我已授予我的应用程序完整的应用程序权限,没有任何更改。我想念什么吗?

1 个答案:

答案 0 :(得分:1)

这是我的第一个答案,希望我能为您提供帮助。

由于v1.0并未实现“ memberOf”扩展,因此您应该使用beta版本,以便将sdk更改为您必须添加的beta版本:

 client.BaseUrl = "https://graph.microsoft.com/beta";

我通常使用QueryOption包含扩展,如下所示:

 List<QueryOption> options = new List<QueryOption> { new QueryOption("$Expand", "MemberOf") };

但是我认为您的使用方式不会有任何问题

尝试以下代码:

var clientCred = new ClientCredential("<client id>",  "<secret>");
var authContext = new AuthenticationContext("https://login.windows.net/" + "<b2c tenant>");
var authResult = authContext.AcquireTokenAsync("https://graph.microsoft.com/", clientCred).Result;
        var client = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    var token = authResult.AccessToken;
                    var result = await Task.FromResult(token);

                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                }));
        //Change to the beta version
        client.BaseUrl = "https://graph.microsoft.com/beta";
        //Parameters of the query
         List<QueryOption> options = new List<QueryOption> { new QueryOption("$Expand", "MemberOf") };
        // query with parameters
         var users = await graphClient.Users.Request(options).GetAsync();