title: Cannot get AzureActiveDirectoryCredentials AccessToken for Azure Mobile Service
body: <p>I have been following the Azure Mobile Services tutorial (<a href="http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-calling-sharepoint-on-behalf-of-user/" rel="nofollow">here</a>) and am having trouble getting my authentication process to work correctly.</p>
  
  <p>I can get my phone application to login correctly, and it appears that the fields are set correctly, however when I get the AzureActiveDirectoryCredentials the AccessToken is null.</p>
  
  <p>The code I am using to get access to Sharepoint is:</p>
  
  <pre><code>    var identities = await user.GetIdentitiesAsync();
      AzureActiveDirectoryCredentials creds = identities.OfType&lt;AzureActiveDirectoryCredentials&gt;().FirstOrDefault();
      return creds == null ? null : new SharepointContext(creds.AccessToken, settings);
  </code></pre>
  
  <p>As the AccessToken is null I cannot create the UserAssertion lower down in the example code, so my service returns a 500 error all the time. The exception generated is:</p>
  
  <pre><code>Exception=System.ArgumentNullException: Value cannot be null.
  Parameter name: assertion
  </code></pre>
  
  <p>The code that generates the exception looks like this:</p>
  
  <pre><code>    AuthenticationContext ac = new AuthenticationContext(authority);
      AuthenticationResult ar = ac.AcquireToken(sharepointURL, new ClientCredential(clientId, clientSecret), new UserAssertion(userToken));
  </code></pre>
  
  <p>I can successfully load data from the table API, so I am confident that my mobile service is correctly configured.</p>
  
  <p>The code I am using to access the Mobile Service is:</p>
  
  <pre><code>JToken result = await App.MobileService.InvokeApiAsync("contacts", HttpMethod.Get, new Dictionary&lt;string, string&gt;());
  </code></pre>
  
  <p>Has anybody had any success getting this to work?</p>