c# - Xamarin.Forms 和 Microsoft Graph 无法在 iOS 设备上运行

标签 c# xamarin xamarin.forms microsoft-graph-api microsoft-graph-sdks

我正在使用 Xamarin.Forms 并尝试在 this tutorial 之后使用 Microsoft Graph API .

这在 iOS 模拟器中完美运行,但是当我在我的实际设备上尝试时,我无法登录。当我单击 WelcomePage.xaml 上的登录按钮时,它完全没有任何反应。 ...有什么我想念的吗?

这是我的登录方法:

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;
}

它在这条线上停止工作:

    var authResult = await interactiveRequest.ExecuteAsync();

这是我的 Entitlements.plist

的来源

enter image description here

最佳答案

iOS 12 存在一个已知问题 documented here :

Microsoft has released a security advisory to provide information about an incompatibility between iOS12 and some types of authentication. The incompatibility breaks social, WSFed, and OIDC logins. This advisory also provides guidance on what developers can do to remove current security restrictions added by ASP.NET to their applications to become compatible with iOS12.

我怀疑行为差异的原因与您设备上的 iOS 版本和模拟器有关。

security advisory文档链接包括一些潜在的修复:

If you are using ASP.NET Core Identity you disable the protection by configuring cookies with the following code

services.ConfigureExternalCookie(options =>
{
    // Other options
    options.Cookie.SameSite = SameSiteMode.None;
});
services.ConfigureApplicationCookie(options =>
{
    // Other options
    options.Cookie.SameSite = SameSiteMode.None;
});

If you are using cookie authentication without ASP.NET Core identity you can turn off the protection with the following code

services.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
    // Other options
    options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
})

关于c# - Xamarin.Forms 和 Microsoft Graph 无法在 iOS 设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078756/

相关文章:

c# - Xamarin 应用程序在重新打开 NullReference 时崩溃

xaml - Xamarin 表单 StackLayout : How to set width/height of children to percentages

c# - 如何处理 xamarin 表单中的 "The GenerateResourceDesigner task failed unexpectedly"错误?

c# - ListView不会删除Xamarin.Forms中的项目。我已将ObservableCollection分配给ListView itemsource。 MVVM

c# - C# 中有任何简单的方法来获取自身的文件路径+文件名吗?

c# - 从循环中创建唯一字符串列表的最快方法?

c# - 如何从 HttpResponseMessage 中读取 cookie?

c# - System.IFormattable.Format(...) 的实现是否应该抛出异常?

c# - 为什么我不能有一个不安全的 volatile 指针,只能在 AdHoc 模式下?

xamarin - 如何以xamarin形式使用消息中心