title: active-directory-xamarin-native-v2 sample project from Microsoft for Xamarin Forms is not working in iOS
body: <p>I am working on office365 authentication , took the code from github, for android build it is working fine, but in iOS not getting data response (token) from office365.</p>
  
  <p>project link - <a href="https://github.com/Azure-Samples/active-directory-xamarin-native-v2" rel="nofollow noreferrer">https://github.com/Azure-Samples/active-directory-xamarin-native-v2</a></p>
  
  <pre><code>public partial class MainPage : ContentPage
  {
      public MainPage()
      {
          InitializeComponent();
      }
  
      async void OnSignInSignOut(object sender, EventArgs e)
      {
          AuthenticationResult authResult = null;
          IEnumerable&lt;IAccount&gt; accounts = await App.PCA.GetAccountsAsync();
          try
          {
              if (btnSignInSignOut.Text == "Sign in")
              {
                  // let's see if we have a user in our belly already
                  try
                  {
                      IAccount firstAccount = accounts.FirstOrDefault();
                      authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount)
                                            .ExecuteAsync();
                      await RefreshUserDataAsync(authResult.AccessToken).ConfigureAwait(false);
                      Device.BeginInvokeOnMainThread(() =&gt; { btnSignInSignOut.Text = "Sign out"; });
                  }
                  catch (MsalUiRequiredException ex)
                  {
                      try
                      {
  
                          authResult = await App.PCA.AcquireTokenInteractive(App.Scopes)
                                                    .WithParentActivityOrWindow(App.ParentWindow)
                                                    .ExecuteAsync();
  
                          await RefreshUserDataAsync(authResult.AccessToken);
                          Device.BeginInvokeOnMainThread(() =&gt; { btnSignInSignOut.Text = "Sign out"; });
                      }
                      catch(Exception ex2)
                      {
                          slUser.IsVisible = true;
                      }
                  }
              }
              else
              {
                  while (accounts.Any())
                  {
                      await App.PCA.RemoveAsync(accounts.FirstOrDefault());
                      accounts = await App.PCA.GetAccountsAsync();
                  }
  
                  slUser.IsVisible = false;
                  Device.BeginInvokeOnMainThread(() =&gt; { btnSignInSignOut.Text = "Sign in"; });
              }
          }
          catch (Exception ex)
          {
  
          }
      }
  
      public async Task RefreshUserDataAsync(string token)
      {
          //get data from API
          slUser.IsVisible = true;
          HttpClient client = new HttpClient();
          HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");
          message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token);
          HttpResponseMessage response = await client.SendAsync(message);
          string responseString = await response.Content.ReadAsStringAsync();
          if (response.IsSuccessStatusCode)
          {
              JObject user = JObject.Parse(responseString);
  
              slUser.IsVisible = true;
  
              Device.BeginInvokeOnMainThread(() =&gt;
              {
  
                  lblDisplayName.Text = user["displayName"].ToString();
                  lblGivenName.Text = user["givenName"].ToString();
                  lblId.Text = user["id"].ToString();
                  lblSurname.Text = user["surname"].ToString();
                  lblUserPrincipalName.Text = user["userPrincipalName"].ToString();
  
                  // just in case
                  btnSignInSignOut.Text = "Sign out";
              });
          }
          else
          {
              await DisplayAlert("Something went wrong with the API call", responseString, "Dismiss");
          }
      }
  }
  }
  </code></pre>
  
  <p>I should get the same as android build, but in iOS not getting token from office365 server, seems like certificate issue, Below lines I found which are need to follow, in order to work in iOS</p>
  
  <p>Also, in order to make the token cache work and have the AcquireTokenSilentAsync work multiple steps must be followed :</p>
  
  <p>1)Enable Keychain access in your Entitlements.plist file and specify in the Keychain Groups your bundle identifier.</p>
  
  <p>2)In your project options, on iOS Bundle Signing view, select your Entitlements.plist file for the Custom Entitlements field.</p>
  
  <p>3) When signing a certificate, make sure XCode uses the same Apple Id.</p>
  
  <p>Above are from Microsoft website </p>
  
  <p>below is Entitlements.plist file details -
  keychain-access-groups I am using $(AppIdentifierPrefix)com.oauth.office365 where com.oauth.office365 is my bundle identifier </p>