让O365用户获得未经授权的异常的后台工作

时间:2018-05-31 14:37:17

标签: c# microsoft-graph

我们正在尝试创建一个微服务模块,其工作是每天使用Microsoft Graph API(/ users /)获取所有用户并使用它们执行某些操作。

我们已按照此处的指南进行操作:https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_service并授予Azure APP Read.User.All权限:enter image description here

然后,我们获得了Azure刀片管理员对此权限的同意。

我们的服务使用Microsoft.Identity.Client lib来获取令牌:

var confidentialClientApplication = new ConfidentialClientApplication(
            this.applicationId,
            this.authority,
            this.redirectUri,
            this.credential,
            new TokenCache(),
            new TokenCache());

            var result = await confidentialClientApplication.AcquireTokenForClientAsync(new[] { "https://graph.microsoft.com/.default" });
            return result.AccessToken;

然后我们创建Graph客户端:

new GraphServiceClient(new DelegateAuthenticationProvider(
            async requestMessage =>
            {
                var graphToken = await this.graphAuthProvider.GetAccessTokenAsync();

                // Append the access token to the request
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", graphToken);
            }));

下面是我回来的令牌的有效负载(省略了一些信息)

{
 "aud": "https://graph.microsoft.com",
 "iss": "https://sts.windows.net/<tenant_id>/",
 "iat": 1527833423,
 "nbf": 1527833423,
 "exp": 1527837323,
 "aio": "Y2dgYKgxbYzssLvLWfve6WbgtsdWAA==",
 "app_displayname": "<app_name>",
 "appid": "<app_id>",
 "appidacr": "1",
 "e_exp": 262800,
 "idp": "https://sts.windows.net/<tenant_id>/",
 "oid": "<subject_id>",
 "sub": "<subject_id>",
 "tid": "<tenant_id>",
 "uti": "-XvIySkmuUOuqmQI0l8CAA",
 "ver": "1.0"
}

然后尝试让用户收到错误消息,说明该应用程序无权访问该资源。

1 个答案:

答案 0 :(得分:1)

发现了什么问题。结果我添加了委托权限而不是应用程序权限。