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中的一些項目。