I am using Xamarin.Forms and trying to use Microsoft Graph API following this tutorial.
This works perfectly in an iOS simulator, but when I try on my actual device, I cannot log in. When I click on the login button on my WelcomePage.xaml
, it does absolutely nothing....is there something I'm missing?
This is my SignIn
Method:
public async Task SignIn()
{
var scopes = OAuthSettings.Scopes.Split(' ');
// First, attempt silent sign in
// If the user's information is already in the app's cache,
// they won't have to sign in again.
string accessToken = string.Empty;
try
{
var accounts = await PCA.GetAccountsAsync();
if (accounts.Count() > 0)
{
var silentAuthResult = await PCA
.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
Debug.WriteLine("User already signed in.");
Debug.WriteLine($"Access token: {silentAuthResult.AccessToken}");
accessToken = silentAuthResult.AccessToken;
}
}
catch (MsalUiRequiredException)
{
// This exception is thrown when an interactive sign-in is required.
Debug.WriteLine("Silent token request failed, user needs to sign-in");
}
if (string.IsNullOrEmpty(accessToken))
{
// Prompt the user to sign-in
var interactiveRequest = PCA
.AcquireTokenInteractive(scopes);
if (AuthUIParent != null)
{
interactiveRequest = interactiveRequest
.WithParentActivityOrWindow(AuthUIParent);
}
var authResult = await interactiveRequest.ExecuteAsync();
Debug.WriteLine($"Access Token: {authResult.AccessToken}");
}
// Initialize Graph client
GraphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
async(requestMessage) =>
{
var accounts = await PCA
.GetAccountsAsync();
var result = await PCA
.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
}));
await GetUserInfo();
IsSignedIn = true;
}
it stops working on this line:
var authResult = await interactiveRequest.ExecuteAsync();
Here is the source of my Entitlements.plist