我測驗了使用圖形 API 在 C# 和 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);
}));
}

uj5u.com熱心網友回復:
在郵遞員的電話中,您是否使用了代碼呼叫中使用的相同令牌?也許有不同的用戶具有不同的權限?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480708.html
