c# - AcquireTokenSilentAsync 无法正常工作,因为 ConfidentialClientApplication 没有用户
问题描述
我的AcquireTokenSilentAsync
方法有问题,希望有人能帮助我。
在下面的方法中,我尝试使用该AcquireTokenSilentAsync
方法,以便以后可以使用它来调用 Microsoft graph api。不幸的是,Users 的属性ConfidentialClientApplication
是空的,cca.AcquireTokenSilentAsync
因为它需要通过调用参数中的第一个用户而失败cca.Users.First()
。
public async Task<string> GetUserAccessTokenAsync()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);
TokenCache userTokenCache = new SessionTokenCache(signedInUserID, httpContext).GetMsalCacheInstance();
ConfidentialClientApplication cca = new ConfidentialClientApplication(
appId,
redirectUri,
new ClientCredential(appSecret),
userTokenCache,
null);
try
{
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes.Split(new char[] { ' ' }), cca.Users.First());
return result.AccessToken;
}
// Unable to retrieve the access token silently.
catch (Exception)
{
HttpContext.Current.Request.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
throw new ServiceException(
new Error
{
Code = GraphErrorCode.AuthenticationFailure.ToString(),
Message = Resource.Error_AuthChallengeNeeded,
});
}
}
}
我不确定为什么ConfidentialClientApplication
不包含任何用户,因为应用程序开始时的登录工作正常。我只在启动时使用UseCookieAuthentication
和。UseOpenIdConnectAuthentication
我希望任何人都可以帮助我解决这个问题!
解决方案
推荐阅读
- c - 使用 Windows API 使用互斥锁方法创建 100 个线程导致意外结果,如何解决?
- npm - 如何使用 npm start 打开 localhost
- html - 为什么不会'justify-content:space-between'使导航栏转到右侧?
- python - 如何检查一个文档中的一行是否适用于同一行,但在另一个文档中
- json - 在django模板中将json字符串转换为日期时间格式dd/mm/yyyy
- html - 无法在 R Shiny 的 HTML 标记中使用“for”关键字
- c# - 如何获取我的控制器的 url 参数?
- go - 无法导入 fmt。(或任何其他基本库)VS Code Go 扩展
- ffmpeg - 将 mp4 转换为 HLS VOD 时可变段大小
- python - 如何在循环中创建多维 json 对象?