【问题标题】:Microsoft graph unable to get required data from .Net core applicationMicrosoft 图形无法从 .Net 核心应用程序获取所需数据
【发布时间】:2021-04-17 09:37:57
【问题描述】:

Net 核心应用程序,我正在尝试做一些基于操作的组和用户。下面的代码我有

public  async Task<GraphServiceClient> GetGraphApiClient()
        {

            var credentials = new ClientCredential(_authenticationConfig.ClientId, _authenticationConfig.ClientSecret);
            var authContext = new AuthenticationContext("https://login.microsoftonline.com/mytenantid");
            var token = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", credentials);
            var accessToken = token.AccessToken;

            var graphServiceClient = new GraphServiceClient(
                new DelegateAuthenticationProvider((requestMessage) =>
                {
                    requestMessage
                .Headers
                .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                    return Task.CompletedTask;
                }));
          
            return graphServiceClient;
        }

我正在尝试如下查询

 GraphServiceClient graphClient = await _myRepository.GetGraphApiClient();
 var user = await graphClient.Me.Request().GetAsync();

这是抛出异常。

  "code": "Authorization_RequestDenied",
  "message": "Insufficient privileges to complete the operation.",

我还在我的 azure 广告应用中设置了所需的权限。我附上了截图。 此外,我也已授予管理员同意。有人可以帮助我在这里缺少什么吗?任何帮助将不胜感激。谢谢

【问题讨论】:

  • 谢谢。请问我为什么要更改申请权限?哦,对了,没有我,即使我也这么想。我可以尝试列出所有组吗?
  • 如果你想使用/me端点,你应该确保使用用户名和密码进行身份验证,而不是clientId/clientSecret。
  • 嗨 Nsevens,请确认我是否必须使用应用程序或委派权限?
  • 不需要用户认证。
  • @Shiva 说的是正确的。

标签: azure asp.net-core azure-active-directory microsoft-graph-api


【解决方案1】:

当您使用客户端凭据流时,您将获得一个 App 令牌,并且该应用程序将充当一个守护程序应用程序。因此,您需要确保使用应用程序权限。如果没有用户进行身份验证,这里/me 没有任何意义吗?所以你需要使用下面的代码来获取特定的用户详细信息。

var user = await graphClient.Users["userid/UPN"].Request().GetAsync();

您可以使用以下代码获取所有用户的 UPN/用户 ID。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多