【发布时间】:2017-12-12 12:59:21
【问题描述】:
我正在开发一个应用程序,此时将检索包含登录用户的 Office 组,并根据该信息执行操作。
我正在使用 oAuth2.0 和 v2.0 令牌端点在没有用户的情况下获得访问权限,并且使用下面的代码,我可以提供管理员同意权限(应用于应用程序权限新的应用程序注册门户https://apps.dev.microsoft.com/ 并出现在 Azure 的企业应用程序部分),向 Azure 请求令牌并接收它,但即使应用了权限和该令牌,我也会收到 403 响应代码(不足权限)从 Graph API 到我尝试执行的任何请求。
这些操作的代码如下:
// Request Admin Consent
HttpRequestMessage adminConsentRequest = new HttpRequestMessage(HttpMethod.Get, "https://login.microsoftonline.com/" + TenantId + "/adminconsent?client_id="+ClientId+"&redirect_uri=https%3A%2F%2Flocalhost%3A44369%2FHome%2F");
var adminConsentResponse = await client.SendAsync(adminConsentRequest);
// Request Token
HttpRequestMessage tokenRequest = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/"+TenantId+"/oauth2/v2.0/token") { Content = new FormUrlEncodedContent(tokenRequestPairs) };
var tokenResponse = await client.SendAsync(tokenRequest);
string tokenResponseBody = await tokenResponse.Content.ReadAsStringAsync();
var deserializedTokenResponse = (JObject)JsonConvert.DeserializeObject(tokenResponseBody);
string accessToken = deserializedTokenResponse["access_token"].Value<string>();
// Call Microsoft Graph API
HttpRequestMessage graphRequest = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/memberOf");
graphRequest.Headers.Add("Authorization", "Bearer "+accessToken);
var graphResponse = await client.SendAsync(graphRequest);
string graphResponseBody = await graphResponse.Content.ReadAsStringAsync();
var deserializedGraphResponse = (JObject)JsonConvert.DeserializeObject(graphResponseBody);
Enterprise Application permissions on Azure
APP Registration Portal permissions
有人可以指导我犯的任何错误吗? 应用了授权令牌和权限后,我不明白为什么会收到 AccessDenied 响应。
我申请权限已经超过 48 小时,所以不是同步问题。
更新:感谢@juunas,我设法重新应用权限,令牌现在显示应用程序门户上应用的所有权限(User.Read.All、Directory.Read.All 和 Group .Read.All),但 API 仍然返回 403 状态码 (Authorization_RequestDenied)。
我尝试了另一个没有 /me 的端点,只是为了确保这不是参考问题,但它也返回 403 状态代码。
有趣的是,正如我所说,该应用程序已在新应用程序门户上注册,它出现在 Azure 上的企业应用程序上,但没有出现在我的应用程序注册上,所以我只能更改新应用程序门户上的权限.应该是这样,因为我用的是新的注册门户?
【问题讨论】:
-
您是否检查过访问令牌,例如 jwt.ms ?为了有效,它应该包含具有必要应用权限的
roles声明。 -
刚试过,角色声明只包含 Group.Read.All 权限(但这是我唯一需要的,所以应该没问题)
-
在
tokenRequestPairs,你有resource = "https://graph.microsoft.com"吗? -
谢谢!我会检查一下,如果我发现了什么,我会在这里发布回复! :)
-
嘿@juunas,通过您的博客文章重新申请权限后,它工作了!您能否将您的评论发布为 anwser,以便我将其标记为已批准?
标签: asp.net-mvc azure microsoft-graph-api http-status-code-403