0
我正在尝试针对Microsoft权限使用OpenIdConnectAuthentication。我可以进行身份验证,但是当尝试获取访问令牌时,通话失败,希望我重新进行身份验证,而我却这么做。我似乎从来没有拉适当的令牌。这是获取访问令牌的代码。ConfidentialClientApplication AcquireTokenSilentAsync总是失败
public async Task<string> GetUserAccessTokenAsync()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
//string signedInUserID = User.Identity.GetUserId();
tokenCache = new MSALSessionCache(
signedInUserID,
HttpContext.Current.GetOwinContext().Environment["System.Web.HttpContextBase"] as HttpContextBase);
var cachedItems = tokenCache.ReadItems(appId); // see what's in the cache
ConfidentialClientApplication cca = new ConfidentialClientApplication(
appId,
redirectUri,
new ClientCredential(appSecret),
tokenCache);
try
{
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes.Split(new char[] { ' ' }));
return result.Token;
}
// Unable to retrieve the access token silently.
catch (MsalSilentTokenAcquisitionException)
{
HttpContext.Current.Request.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
//throw new Exception("Resource.Error_AuthChallengeNeeded");
return null;
}
}
我不知道我错过了什么。到目前为止,我已经使用Microsoft Graph REST ASPNET Connect示例来指导我。我的最终目标是验证用户,然后使用他们的个人资料和MS休息API中的一些项目。