2017-06-20 2 views
1

J'essaie d'authentifier l'API Microsoft Graph avec ADAL et obtenir l'exception suivante quand je publierai à Azure:Exception tentative d'authentification Graphique Client sur Azure Publish: « Echec d'acquisition de jeton en silence méthode d'appel AcquireToken. »

« Echec d'acquisition de jeton en silence. appel de méthode AcquireToken »

Il fonctionne très bien quand je lance localement.

 public GraphServiceClient GetAuthGraphClient() 
     { 

     string graphResourceID = "https://graph.microsoft.com/"; 

     return new GraphServiceClient(
      new DelegateAuthenticationProvider(async (requestMessage) => 
      { 
       string accessToken = await GetTokenForApplication(graphResourceID); 
       requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken); 
      } 
      )); 

     } 
     public async Task<string> GetTokenForApplication(string graphResourceID) 
     { 
     string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
     string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
     string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

     try { 
      // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc) 
      ClientCredential clientcred = new ClientCredential(clientId, appKey); 
      // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database 
      AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID)); 
      AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceID, clientcred, UserIdentifier.AnyUser); 
      return authenticationResult.AccessToken; 
     } 
     catch (Exception e) 
     { 
       // Capture error for handling outside of catch block 
       ErrorMessage = e.Message; 

      return null; 
     } 

    } 

Mise à jour par @Fei Xue J'ai mis à jour mon code au-dessous:

public string GetTokenForApplication(string graphResourceID) 
    { 
    string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
    string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
    string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 
    string authority = "https://login.microsoftonline.com/" + tenantID; 


    try { 
     // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc) 
     ClientCredential clientcred = new ClientCredential(clientId, appKey); 
     // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database 
     AuthenticationContext authenticationContext = new AuthenticationContext(authority); 
     var token = authenticationContext.AcquireTokenAsync(graphResourceID, clientcred).Result.AccessToken; 
     return token; 
    } 
    catch (Exception e) 
    { 
     ErrorMessage = e.Message; 
     return null; 
    } 

    } 
    public GraphServiceClient GetAuthGraphClient() 
    { 
    string graphResourceID = "https://graph.microsoft.com/"; 

    return new GraphServiceClient(
     new DelegateAuthenticationProvider((requestMessage) => 
     { 
      string accessToken = GetTokenForApplication(graphResourceID); 
      requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken); 
      return Task.FromResult(0); 
     } 
     )); 
    } 

Ce qui suit est la sortie donc je pense qu'il est de retour jeton avec succès maintenant:

iisexpress.exe Information: 0 : 6/20/2017 12:23:36 PM: 29c5558f-e33b-4312-b235-612909ac1309 - AcquireTokenHandlerBase: === Token Acquisition finished successfully. An access token was retuned: 
    Access Token Hash: <a long access token> 
    Refresh Token Hash: [No Refresh Token] 
    Expiration Time: 6/20/2017 1:23:34 PM +00:00 
    User Hash: null 

Cependant, maintenant, je ne pense pas que j'obtiens le profil d'utilisateur correctement. Est-ce correct?

public void GetUserProfile(GraphServiceClient graphClient) 
{ 
    this.User = Task.Run(async() => { return await graphClient.Me.Request().GetAsync(); }).Result; 
} 

Parce que la propriété de l'utilisateur de mon viewmodel ne reçoit aucune donnée.

Répondre

1

méthode tente d'acquérir un jeton à partir du cache ou d'actualiser le jeton d'accès à l'aide du jeton d'actualisation.

Si vous développiez un service, vous pouvez envisager d'utiliser le client credentials flow pour s'authentifier auprès d'Azure AD. Voici le code pour votre référence:

string authority = "https://login.microsoftonline.com/{tenant}"; 

string clientId = ""; 
string secret = ""; 
string resource = "https://graph.microsoft.com/"; 

var credential = new ClientCredential(clientId, secret); 
AuthenticationContext authContext = new AuthenticationContext(authority); 

var token = authContext.AcquireTokenAsync(resource, credential).Result.AccessToken; 

En-outre, s'il vous plaît assurez-vous que vous avez l'autorisation config assez pour appeler le Microsoft Graph REST correspondant. Vous pouvez consulter le lien ci-dessous à propos de l'autorisation détaillée:

Microsoft Graph permissions reference

+0

Merci Fei Xue! J'ai mis à jour mon post ci-dessus pour montrer ce qui se passe après la mise à jour de mon code à votre suggestion. On dirait qu'il obtient un jeton d'accès avec succès maintenant. J'ai également assez d'autorisations pour appeler le REST Graph. –

+0

@ProperNouns Si le dépouillement est utile pour résoudre le problème, acceptez-le comme réponse afin que d'autres communautés ayant le même problème puissent facilement reconnaître la solution. –

+0

Xue, j'ai marqué comme la bonne réponse. S'il vous plaît voir ma mise à jour ci-dessus. Est-ce que j'appelle le profil d'utilisateur correctement? Devrais-je poser cette question dans une autre question? –