goo*_*ate 9 c# office365 microsoft-graph azure-ad-msal
我正在使用MSAL并且有一位用户收到以下错误:
{
"error":{
"code":"ResourceNotFound",
"message":"Resource could not be discovered.",
"innerError":{
"request-id":"99b44a33-e5cd-4b69-9730-32d72e1f4ebf",
"date":"2016-12-11T03:51:37"
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码是默认的MSAL演示代码:
public async Task<ActionResult> ReadMail()
{
try
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
ConfidentialClientApplication cca = new ConfidentialClientApplication(clientId, null,
new ClientCredential(appKey), new MSALSessionCache(signedInUserID, this.HttpContext));
string[] scopes = { "Mail.Read" };
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes);
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", result.Token);
HttpResponseMessage hrm = await hc.GetAsync("https://graph.microsoft.com/v1.0/me/messages");
string rez = await hrm.Content.ReadAsStringAsync();
ViewBag.Message = rez;
return View();
}
catch (MsalSilentTokenAcquisitionException)
{
ViewBag.Relogin = "true";
return View();
}
catch (Exception eee)
{
ViewBag.Error = "An error has occurred. Details: " + eee.Message;
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
事实证明,整合是这样的:
题
我应该如何针对上述情况进行防御性编码?(或类似的混合情况)?
老问题,但似乎其他人也遇到过这个问题。
这实际上并不是 MSAL 问题,因为令牌本身并不是这里的问题。该异常源于https://graph.microsoft.com/v1.0/me/messages
用户没有可访问邮箱时的调用。
user
通常,您可以通过查看该用户的provisionedPlans
集合来确定已将哪些资源分配给该用户:
https://graph.microsoft.com/v1.0/me?$select=provisionedPlans
Run Code Online (Sandbox Code Playgroud)
这将返回已为用户配置的集合资源。例如,该用户没有配置 Exchange:
https://graph.microsoft.com/v1.0/me?$select=provisionedPlans
Run Code Online (Sandbox Code Playgroud)
将此与配置了 Excange 的用户进行比较:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(provisionedPlans)/$entity",
"provisionedPlans": [
{
"capabilityStatus": "Enabled",
"provisioningStatus": "Success",
"service": "SharePoint"
},
{
"capabilityStatus": "Enabled",
"provisioningStatus": "Success",
"service": "SharePoint"
}
]
}
Run Code Online (Sandbox Code Playgroud)
也可能有用的类似属性是assignedPlans
。这将返回已分配给的资源user
,可用于确定是否尚未进行预配或未正式“预配”的资源(例如 Microsoft Teams)。
也就是说,您应该始终针对用户没有或缺乏您所请求的资源的权限的可能性进行防御性编码。在多种情况下,资源可能由于中断或用户权限而无法访问,处理这些情况的唯一可靠方法是通过可靠的异常处理进行支持。
归档时间: |
|
查看次数: |
293 次 |
最近记录: |