【发布时间】:2020-12-29 10:35:28
【问题描述】:
我们有一个 NET Core 3.1 Web 应用程序,用户在 Azure AD 中使用 Microsoft.Identity.Web 包进行身份验证。我们代表已登录用户调用 Microsoft Graph SDK,如 here 所述。
登录到应用程序后一切正常 - 对 Microsoft Graph SDK 的所有调用都成功。但是,大约 30 到 60 分钟后(我们还没有准确计时)用户在我们的应用程序中保持身份验证,但对 Microsoft Graph SDK 的调用失败并出现以下错误:
IDW10502: An MsalUiRequiredException was thrown due to a challenge for the user.
内部异常有:
Microsoft.Identity.Client.MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.
然后,用户需要退出应用程序并重新登录,然后在 30-60 分钟内再次成功调用 MS Graph。
任何人都可以对此有所了解吗?
我们的设置
在 appsettings.json 中:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "<our-domain-name>",
"TenantId": "<our-tenant-id>",
"ClientId": "<our-client-id>",
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath ": "/signout-callback-oidc",
"ClientSecret": "<secret>"
},
"GraphBeta": {
"BaseUrl": "https://graph.microsoft.com/beta",
"Scopes": "User.Read Sites.Read.All Files.Read.All Sites.ReadWrite.All Files.ReadWrite.All",
"DefaultScope": "https://graph.microsoft.com/.default"
}
在 Startup.cs 中:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp( Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { "User.Read","Sites.Read.All","Files.Read.All","Sites.ReadWrite.All","Files.ReadWrite.All" })
.AddMicrosoftGraph(Configuration.GetSection("GraphBeta"))
.AddDistributedTokenCaches();
services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = Configuration.GetConnectionString("AzureConnection");
options.SchemaName = "dbo";
options.TableName = "TokenCache";
});
GraphServiceClient 被注入到我们的控制器中:
readonly ITokenAcquisition tokenAcquisition;
private readonly GraphServiceClient graphServiceClient;
public SearchController(IConfiguration configuration, ITokenAcquisition _tokenAcquisition,
GraphServiceClient _graphServiceClient) : base(configuration)
{
tokenAcquisition = _tokenAcquisition;
graphServiceClient = _graphServiceClient;
}
以及用AuthorizeForScopes装饰的相关动作:
[AuthorizeForScopes(ScopeKeySection = "GraphBeta:Scopes")]
public async Task<IActionResult> Results(string queryString, int pageNumber, int pageSize)
{
...
}
然后调用MS Graph,例如:
return await graphClient.Search.Query(requests).Request().PostAsync();
我们正在使用
- Microsoft.Identity.Web 1.4.1
- Microsoft.Identity.Web.MicrosoftGraphBeta 1.4.1
- Microsoft.Graph.Beta 0.35.0-preview
- Microsoft.Graph.Auth 1.0.0-preview.6
【问题讨论】:
-
这个错误背后的原因可能是因为acquiretokensilent调用可能没有有效的缓存细节了。有关 acquiretokensilent 的更多详细信息,请参阅以下链接。 docs.microsoft.com/en-us/azure/active-directory/develop/…。您在以下链接中有类似的讨论。 docs.microsoft.com/en-us/answers/questions/4505/…
-
感谢@DinakarJ-MSFTIdentity 的回复确实,acquiretokensilent 调用似乎是根本原因,并且引发了 MsalUiRequiredException,但不幸的是,您发送的链接没有导致解决方案。显然 AuthorizeForScopes 属性应该捕获并处理 MsalUiRequiredException,但在我的情况下它不是。似乎无法找到它是否是我错过的东西,或者问题是否出在 Microsoft.Identity.Web 或其他地方。
标签: c# azure asp.net-core microsoft-graph-sdks microsoft-identity-platform