Wir arbeiten an einem ASP.NET MVC-Projekt mit der Microsoft Graph-API. Es basiert stark auf dem Beispielcode unter https://github.com/microsoftgraph/aspnet-snippets-sample. Die App funktioniert zunächst gut - wir können uns mit unserem Mieterkonto anmelden und Daten aus dem Diagramm abrufen. Wenn wir jedoch eine neue Sitzung von Visual Studio starten oder, wenn wir nur eine Weile warten, AcquireTokenSilentAsync
wirftFehler beim automatischen Abrufen des Tokens - Microsoft Graph API
Fehlgeschlagen Token still Cache
Wenn der Web-Browser gelöscht wird es funktioniert wieder zu erwerben, aber Nach einer Weile kommt der Fehler zurück. Wir haben versucht, die Autorität auf Allgemein, Organisationen und Mieter zu ändern, aber der Fehler bleibt bestehen.
Unsere Methode das Zugriffstoken für das Erhalten ist wie folgt:
// Gets an access token and its expiration date. First tries to get the token from the token cache.
public async Task<string> GetUserAccessTokenAsync()
{
// Initialize the cache.
HttpContextBase context = HttpContext.Current.GetOwinContext().Environment["System.Web.HttpContextBase"] as HttpContextBase;
tokenCache = new SessionTokenCache(
ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value,
context);
IEnumerable<TokenCacheItem> cachedItems = tokenCache.ReadItems(appId); // see what's in the cache
if (cachedItems.Count() > 0)
return cachedItems.First().Token;
if (!redirectUri.EndsWith("/")) redirectUri = redirectUri + "/";
string[] segments = context.Request.Path.Split(new char[] { '/' });
ConfidentialClientApplication cca = new ConfidentialClientApplication(
appId,
redirectUri + segments[1],
new ClientCredential(appSecret),
tokenCache);
string allScopes = nonAdminScopes;
string[] scopes = allScopes.Split(new char[] { ' ' });
try
{
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes);
return result.Token;
}
// Unable to retrieve the access token silently.
catch (MsalSilentTokenAcquisitionException)
{
HttpContext.Current.Request.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = redirectUri + segments[1] },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
throw new ServiceException(
new Error
{
Code = GraphErrorCode.AuthenticationFailure.ToString(),
Message = Resource.Error_AuthChallengeNeeded,
});
}
}
Irgendwelche Ideen, warum das Token still werden nicht erfasst?