簡體   English   中英

azure 廣告讓所有用戶使用圖 api

[英]azure ad get all users using graph api

我測試使用圖表 API 獲取 C# 和 Postman 中的所有用戶信息。

在 postman 中,它正確響應。 但是,在 c# 中,它只是響應登錄用戶。

這是c#和postman中的一個方法

GET https://graph.microsoft.com/v1.0/users
// Load configuration settings from PrivateSettings.config
private static string appId = ConfigurationManager.AppSettings["ida:AppId"];
private static string appSecret = ConfigurationManager.AppSettings["ida:AppSecret"];
private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
private static List<string> graphScopes =
            new List<string>(ConfigurationManager.AppSettings["ida:AppScopes"].Split(' '));

// Returns all of the users in the directory of the signed-in user's tenant. 
public static async Task<IGraphServiceUsersCollectionPage> GetUsersAsync()
{
    IGraphServiceUsersCollectionPage users = null;

    try
    {
        var graphClient = GetAuthenticatedClient();
        users = await graphClient.Users.Request().GetAsync();
        foreach (var user in users)
        {
            Debug.WriteLine("User: " + user.DisplayName);
        }
        return users;
    }
    catch (ServiceException e)
    {
        Debug.WriteLine("We could not get users: " + e.Error.Message);
        return null;
    }
}

private static GraphServiceClient GetAuthenticatedClient()
{
    return new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        var idClient = ConfidentialClientApplicationBuilder.Create(appId)
                            .WithRedirectUri(redirectUri)
                            .WithClientSecret(appSecret)
                            .Build();

                        var tokenStore = new SessionTokenStore(idClient.UserTokenCache,
                                HttpContext.Current, ClaimsPrincipal.Current);

                        var userUniqueId = tokenStore.GetUsersUniqueId(ClaimsPrincipal.Current);
                        var account = await idClient.GetAccountAsync(userUniqueId);

                // By calling this here, the token can be refreshed
                // if it's expired right before the Graph call is made
                var result = await idClient.AcquireTokenSilent(graphScopes, account)
                            .ExecuteAsync();

                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    }));
        }

在此處輸入圖像描述 在此處輸入圖像描述

在來自 Postman 的調用中,您是否使用了代碼調用中使用的相同令牌? 也許有不同的用戶具有不同的權限?

暫無
暫無

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

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