所以我正在尝试为我们的 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));
异常是:“无法静默获取 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 得到的结果
我已将 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/