c# - 无法在 ASP.NET MVC 上使用 ADAL for Office 365 REST API 静默验证用户

标签 c# asp.net office365 adal office365api

所以我正在尝试为我们的 office 身份验证实现持久性 token ,这样用户就不必在每次进入新 session 时都登录 office。我目前必须验证用户的代码如下。

string authority = "https://login.microsoftonline.com/common";
var tokenCache = new ADALTokenCache(User.Identity.GetUserId());
AuthenticationContext authContext = new AuthenticationContext(authority, tokenCache );
var token = authContext.AcquireTokenSilentAsync(scopes, clientId, new UserIdentifier(userId, UserIdentifierType.RequiredDisplayableId));

但是到目前为止我尝试过的一切都给我以下错误 enter image description here

异常是:“无法静默获取 token 。调用方法 AcquireToken”

我首先使用的获取token的方法如下

string authority = "https://login.microsoftonline.com/common";
var fileCache = new ADALTokenCache(User.Identity.GetUserId());
AuthenticationContext authContext = new AuthenticationContext(authority, fileCache);
var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
                authCode, redirectUri, credential, scopes);

我使用的 token 缓存是我从我无法再次找到的教程中制作的数据库实现,如果我观察数据库,我可以看到当 AcquireTokenByAuthorizationCodeAsync 被调用。

更新:
这是我在调用 AcquireTokenByAuthorizationCodeAsync 时从 authResult 得到的结果 enter image description here

我已将 Virbonet 的答案标记为解决方案,但我没有修复它,但他确实向我解释了我哪里出错了

最佳答案

AcquireTokenSilent 如果您在权限中传递/common 则无法工作。使用“common”相当于声明您不知道用户来自哪个租户,因此 ADAL 无法从特定租户返回缓存 token - 需要用户交互来确定应使用哪个租户。 如果您想调用 AcquireTokenSilent,您需要使用传入用户的确切租户初始化权限,如 "https://login.microsoftonline.com/"+tenantID 这里的 tenantID 是来自当前 ClaimsPrincipal 的 tenantID。

关于c# - 无法在 ASP.NET MVC 上使用 ADAL for Office 365 REST API 静默验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35565370/

相关文章:

calendar - 如何通过 MS Graph 创建以房间为位置的事件?

c# - 当应用程序进程被终止/崩溃时终止子进程

c# - 如何找到引用数据类型

c# - 更改 IsolatedStorage 位置

c# - 连接尝试失败,因为连接方未正确响应

html - Bootstrap 响应式表格水平滚动始终可见

ios - iOS 设备上的 Azure 媒体播放器

c# - asp net core viewmodel参数转换为字符串url

c# - 将 Task.Run() 用于 Linq - 优化或瓶颈

office365 - 当 ResourceURI 包含结尾斜杠时,Microsoft Graph Teams API 不起作用