Xamarin:Microsoft登录时出现iOS身份验证错误

时间:2020-01-06 08:21:12

标签: c# ios xamarin.forms xamarin.ios microsoft-graph-api

即使使用Microsoft登录成功登录后,我仍收到此错误对话框。 enter image description here

这是验证代码:

#region IMicrosoftLogin implementation
        public async System.Threading.Tasks.Task<Xamarin.Auth.Account> LoginAsync()
        {
            window = UIApplication.SharedApplication.KeyWindow;
            viewController = window.RootViewController;

            var auth = new OAuth2Authenticator(
             "key_here",//for non- prod
                                                    //for production
              "openid email https://graph.microsoft.com/user.read",
              new Uri("https://login.microsoftonline.com/common/oauth2/V2.0/authorize"),
              new Uri("https://myapp_redirect_url"),// for non- prod
              null
          )
            {
                AllowCancel = true
            };
            auth.Completed += Microsoft_Auth_Completed;

            var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();
            d1 = (o, e) =>
            {
                try
                {

                    if (e.IsAuthenticated)
                    {
                        viewController.DismissViewController(true, null);
                        tcs1.TrySetResult(e);
                    }
                    else
                    {
                        viewController.DismissViewController(true, null);
                    }
                }
                catch (Exception)
                {
                    tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));
                }
            };

            try
            {
                auth.Completed += d1;
                if (viewController == null)
                {
                    while (viewController.PresentedViewController != null)
                        viewController = viewController.PresentedViewController;
                    viewController.PresentViewController(auth.GetUI(), true, null);
                }
                else
                {
                    viewController.PresentViewController(auth.GetUI(), true, null);
                    UserDialogs.Instance.HideLoading();

                }
                var result = await tcs1.Task;
                return result.Account;

            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                auth.Completed -= d1;
            }

            //auth.Error += (object sender, AuthenticatorErrorEventArgs eventArgs) => {
            //    auth.IsEnabled = false;
            //};
        }

        private void Microsoft_Auth_Completed(object sender, AuthenticatorCompletedEventArgs e)
        {  /// Break point here is not getting triggered.
            var authenticator = sender as OAuth1Authenticator;

            if (authenticator != null)
            {
                authenticator.Completed -= Microsoft_Auth_Completed;
            }

            if (e.IsAuthenticated)
            {
                var a = e.Account;
            }
            else
            {
            }
        }

登录异步调用按钮点击,如下所示:

btnSignIn.Clicked += async (object sender, EventArgs e) =>
            {
                if (networkConnection != null && networkConnection.CheckNetworkConnection())
                {
                    UserDialogs.Instance.ShowLoading("Loading", null);
                    var loginresult = await MicrosoftLogin.LoginAsync();
.....

MicrosoftLogin.cs

namespace projectnamescpace
{
    public interface IMicrosoftLogin
    {
        Task<Account> LoginAsync();
    }
}

请帮助我。

我已经看到了以下链接解决方案,但这些解决方案对我不起作用。

  1. https://forums.xamarin.com/discussion/5866/xamarin-auth-and-infinite-error-alerts
  2. Authentication Error e.Message = OAuth Error = Permissions+error
  3. https://forums.xamarin.com/discussion/95176/forms-oauth-error-after-authenticated-unable-to-add-window-token-android-os-binderproxy

1 个答案:

答案 0 :(得分:0)

此问题可能是由于“区域策略”阻止了主机名。

您也可以通过为Mac修改DNS(例如,以8.8.8.8为例)来解决此问题。

  1. 您的设备,设置/ Wi-Fi
  2. 选择已连接的Wi-Fi平板电脑
  3. 按DHCP / DNS
  4. 设置为8.8.8.8

或者您可以将电话连接到VPN,以便将应用程序部署到设备上以查看公司服务器。