0

I am getting this error dialog even after logging in successfully using Microsoft login. enter image description here

Here is the code to authenticate :

#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
            {
            }
        }

Login async called on button click like this :

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

Please help me.

I have already saw following link solutions and they aren't working for me.

  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
Shruti
  • 1
  • 13
  • 55
  • 95

1 Answers1

0

The issue might be caused by the HostName been blocked because of Area Policy .

You could solve this by modifying the DNS (to 8.8.8.8 as an example) for your Mac as well.

  1. Your device, Settings/Wi-Fi
  2. Choose connected Wi-Fi pot
  3. Press DHCP/DNS
  4. Set to 8.8.8.8

Or you could connect phone to the VPN for your apps deployed to device to see corporate servers.

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22