title: AcquireTokenSilent is Failing to acquire token silently?
body: <p>I have a hosted application on Azure. In the SiteMaster page (master page),  I am trying to get all the user's AD groups because my application is a role-based application where each user belongs to an Azure group and each group can perform certain functionalities.</p>
  
  <p>My code in the page load event is like the following:</p>
  
  <pre><code>private static string clientId = ConfigurationManager.AppSettings["ida:ClientID"];
  private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
  private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
  private static string graphResourceId = "https://graph.windows.net";
  
  protected void Page_Load(object sender, EventArgs e)
  {
        if (Request.IsAuthenticated)
        {
            IList&lt;string&gt; groups = GetUserData();
        }
  }
  </code></pre>
  
  <p>Notice that I am calling the function 'GetUserData()' that actually brings all the groups that a user belongs to. The code of this function is like the following:</p>
  
  <pre><code>        public IList&lt;string&gt; GetUserData()
          {
              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;
  
              Uri servicePointUri = new Uri(graphResourceId);
              Uri serviceRoot = new Uri(servicePointUri, tenantID);
              ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
                      async () =&gt; await GetTokenForApplication());
  
              IList&lt;string&gt; groupMembership = new List&lt;string&gt;();
              // use the token for querying the graph to get the user details
              IUser user = activeDirectoryClient.Users
                  .Where(u =&gt; u.ObjectId.Equals(userObjectID))
                  .ExecuteAsync().Result.CurrentPage.ToList().First();
              var userFetcher = (IUserFetcher)user;
              requestor = user.DisplayName;
              IPagedCollection&lt;IDirectoryObject&gt; pagedCollection = userFetcher.MemberOf.ExecuteAsync().Result;
              do
              {
                  List&lt;IDirectoryObject&gt; directoryObjects = pagedCollection.CurrentPage.ToList();
                  foreach (IDirectoryObject directoryObject in directoryObjects)
                  {
                      if (directoryObject is Group)
                      {
                          var group = directoryObject as Group;
                          groupMembership.Add(group.DisplayName);
                      }
                  }
                  pagedCollection = pagedCollection.GetNextPageAsync().Result;
              } while (pagedCollection != null);
  
              return groupMembership;
          }
  </code></pre>
  
  <p>The 'GetUserData()' function is calling another function called 'GetTokenForApplication()' which is responsible of getting a token form Azure. The source code of the previous function is like the following:</p>
  
  <pre><code>  protected async Task&lt;string&gt; GetTokenForApplication()
      {
          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;
  
          // 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 EF DB
          AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
          AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceId, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
          return authenticationResult.AccessToken;
  
  
      } 
  </code></pre>
  
  <p>My problem is sometimes when a user tries to login to my application s/he gets the error <code>Failed to acquire token silently. Call method AcquireToken</code></p>
  
  <p>This error happens in certain cases that I don't know and when it happens, it doesn't affect all the users. Some users are still able to get in and not get this error. Any suggestion or idea of why is this happening and how to solve an error like that?</p>
  
  <p>Here is my Stack Trace:</p>
  
  <pre><code>[AdalSilentTokenAcquisitionException: Failed to acquire token silently. Call method AcquireToken]
     Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenSilentHandler.SendTokenRequestAsync() +98
     Microsoft.IdentityModel.Clients.ActiveDirectory.&lt;RunAsync&gt;d__0.MoveNext() +1807
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     Microsoft.IdentityModel.Clients.ActiveDirectory.&lt;AcquireTokenSilentCommonAsync&gt;d__10.MoveNext() +317
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     Microsoft.IdentityModel.Clients.ActiveDirectory.&lt;AcquireTokenSilentAsync&gt;d__5c.MoveNext() +268
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     MRTWebApplication.&lt;GetTokenForApplication&gt;d__6.MoveNext() +539
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     MRTWebApplication.&lt;&lt;GetUserData&gt;b__5_0&gt;d.MoveNext() +194
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     Microsoft.Azure.ActiveDirectory.GraphClient.Extensions.&lt;SetToken&gt;d__1.MoveNext() +207
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     Microsoft.Azure.ActiveDirectory.GraphClient.Extensions.&lt;ExecuteAsync&gt;d__4d`2.MoveNext() +986
     System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
     Microsoft.Azure.ActiveDirectory.GraphClient.Extensions.&lt;&lt;ExecuteAsync&gt;b__0&gt;d__2.MoveNext() +263
  
  [AggregateException: One or more errors occurred.]
     System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) +4465776
     MRTWebApplication._Default.GetUserData() +778
     MRTWebApplication._Default.Page_Load(Object sender, EventArgs e) +43
     System.Web.UI.Control.OnLoad(EventArgs e) +109
     System.Web.UI.Control.LoadRecursive() +68
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4498
  </code></pre>
  
  <p>Thanks!</p>