簡體   English   中英

Microsoft.Graph.GraphServiceClient 如何限制返回的屬性?

[英]Microsoft.Graph.GraphServiceClient how to limit properties returned?

我正在使用 Azure AD B2C。 我希望計算用戶數量 - 但是,根據文檔

Azure AD B2C 當前不支持目錄對象的高級查詢功能。 這意味着不支持 $count...

太好了,所以我認為下一個最好的辦法是執行查詢以獲取所有用戶的 ID,即:

var options = new TokenCredentialOptions
    {
       AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

var clientSecretCredential = new ClientSecretCredential( tenant_id, client_id, client_secret, options );

var scopes = new[] { "https://graph.microsoft.com/.default" };

var users = await graphClient.Users
                  .Request()
                  .Select( "id" )
                  .GetAsync();

// This shows other properties returned in addition to "id" ...

if ( users is not null )
{
   Console.WriteLine( JsonSerializer.Serialize( users ) );
}

我的印象是(從文檔中) using.Select( "id" ) 只會返回 "id" 屬性,相反,圖形客戶端返回我認為除了 "我死:

[
{"accountEnabled":null,
 "ageGroup":null, 
 ...more properties...,
 "id":"xxxx...",
 "@odata.type":"microsoft.graph.user"
}, 
{"accountEnabled":null,
 "ageGroup":null, 
 ... more properties ...,
 "id":"xxxx...",
 "@odata.type":"microsoft.graph.user"
}, ...
]

我可能做錯了什么,但你如何只返回屬性(在本例中為“id”)而不返回任何其他屬性? 我還沒有找到描述如何執行此操作的示例或文檔。

提前致謝。

我嘗試在我的環境中重現相同的內容並成功獲得如下結果:

為了獲得Azure AD b2c 用戶 ID ,我使用了以下代碼:

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "b2corg.onmicrosoft.com";
var clientId = "ClientID";

var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var userName = "admin@b2corg.onmicrosoft.com";
var password = "password";

var userNamePasswordCredential = new UsernamePasswordCredential(
    userName, password, tenantId, clientId, options);

var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);
var users = await graphClient.Users
           .Request()
           .Select("id")
           .GetAsync();

users.ToList().ForEach(x => { Console.WriteLine("\nid: " + x.Id); });

在此處輸入圖像描述

我也在 Microsoft Graph Explorer 中嘗試過,我只能獲取如下用戶 ID:

https://graph.microsoft.com/v1.0/users?$select=id

在此處輸入圖像描述

感謝@user2250152 和@Rukmini - “答案”本身就是圖 QUERY 僅返回選定/請求的屬性,也就是說,當使用以下代碼時:

var users = await graphClient.Users
           .Request()
           .Select("id")
           .GetAsync();

QUERY 返回“ids”,如@Rukmini 的回答所示,但隨后 graphClient 填充調用完成時返回的“用戶”對象列表。 User 對象將包含 Id 屬性以及與 @user2250152 提到的用戶 object 關聯的所有其他屬性。

再次感謝您解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM