Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure AD B2C和Graph:无法列出memberOf的用户_C#_.net_Azure_Azure Ad B2c_Azure Ad Graph Api - Fatal编程技术网

C# Azure AD B2C和Graph:无法列出memberOf的用户

C# Azure AD B2C和Graph:无法列出memberOf的用户,c#,.net,azure,azure-ad-b2c,azure-ad-graph-api,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 = authCont

我试图列出我的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();

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

这是我的第一个答案,希望我能帮助你

您应该使用测试版,因为v1.0没有实现“memberOf”扩展,要将sdk更改为您必须添加的测试版:

 client.BaseUrl = "https://graph.microsoft.com/beta";
我通常包括expand using QueryOption,如下所示:

 List<QueryOption> options = new List<QueryOption> { new QueryOption("$Expand", "MemberOf") };
List options=newlist{newqueryoption(“$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();
var clientCred=newclientcredential(“,”);
var authContext=新的AuthenticationContext(“https://login.windows.net/" + "");
var authResult=authContext.AcquireTokenAsync(“https://graph.microsoft.com/“,clientCred)。结果;
var client=新的GraphServiceClient(
新的DelegateAuthenticationProvider(
异步(请求消息)=>
{
var token=authResult.AccessToken;
var result=wait Task.FromResult(令牌);
requestMessage.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
}));
//更改为测试版
client.BaseUrl=”https://graph.microsoft.com/beta";
//查询的参数
列表选项=新列表{newqueryoption(“$Expand”,“MemberOf”)};
//带参数查询
var users=await-graphClient.users.Request(options).GetAsync();

你的意思是“然而,这并不像预期的那样有效吗?”?我写了“它有效”;不是:“不”。长话短说:当我要求一个用户时,我可以得到他的群组。当我尝试列出所有用户时,我不能。一个好的答案应该包括一些问题和/或解决方案的解释。如果没有其他问题,请说明您对代码做了哪些更改。@Sergio:这让我更进一步了,但并没有达到预期的方向;)添加
$Expand
会导致
Microsoft.Graph.ServiceException:code:Service\u InternalServerError
。另一方面:当我删除EXPLAND子句时,用户包含许多我也无法获取的其他数据(如
AdditionalData
中的自定义用户属性)。另外,在我的生产环境中使用以“beta”结尾的东西时,我的心理也有问题;)@Krzysztof在我的例子中,这个查询不返回任何错误,如果memberOf的值为。我知道beta版不是获得该值的最佳方式,但我担心如果您不想进行附加查询,您应该使用它。如果我的回答不能帮助你,我很抱歉。@SergioPerales:我应该做更多的研究。我只是偶然发现了这一点,它基本上证明了你的观点。
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();