1
votes

I am trying to authenticate the user while using Microsoft Graph and keep getting the error Failed to acquire token silently. Call method AcquireToken.

Any ideas how to fix this?

try
{
    AuthenticationResult result =
        await authContext.AcquireTokenSilentAsync(SettingsHelper.GraphResourceId,
            clientCredential,
            userId);
    return result.AccessToken;
}
// Unable to retrieve the access token silently.
catch (AdalException ex)
{
    HttpContext.Current.Request.GetOwinContext().Authentication.Challenge(
        new AuthenticationProperties() { RedirectUri = "/" },
        OpenIdConnectAuthenticationDefaults.AuthenticationType);

    throw new Exception(Resource.Error_AuthChallengeNeeded + $" {ex.Message}");
}
1

1 Answers

1
votes

With ADAL, your app will get the access and refresh token the first time an end user logs in by using OpenID Connect ASP.Net OWIN middleware and ADAL .Net . See code sample here , in OnAuthorizationCodeReceived notification , it will acquire the access token and refresh token and places them in a cache instance.

Next time you can use AcquireTokenSilentAsync to acquire security token without asking for user credential. It will check the token cache and confirm the access token's lifetime, and if still valid, return back that access token from the cache. If it sees it is expired, then it will use the refresh token to get you a new access token.

But if you call AcquireTokenSilentAsync before the time that the application first acquires access and refresh tokens at sign in time and places them in a cache instance , or if you fail to initialize AuthenticationContext with the exact same cache instance, the request will fail. Then you should issue a new OWIN challenge as your code shown.