protected override Task<string> LoginAsyncOverride()
        {
            var tcs = new TaskCompletionSource<string>();

            var auth = new WebRedirectAuthenticator (StartUri, EndUri);
            auth.ClearCookiesBeforeLogin = false;

            Intent intent = auth.GetUI (this.context);

            auth.Error += (sender, e) =>
            {
                string message = String.Format (CultureInfo.InvariantCulture, Resources.IAuthenticationBroker_AuthenticationFailed, e.Message);
                InvalidOperationException ex = (e.Exception == null)
                    ? new InvalidOperationException (message)
                    : new InvalidOperationException (message, e.Exception);

                tcs.TrySetException (ex);
            };

            auth.Completed += (sender, e) =>
            {
                if (!e.IsAuthenticated)
                    tcs.TrySetException (new InvalidOperationException (Resources.IAuthenticationBroker_AuthenticationCanceled));
                else
                    tcs.TrySetResult(e.Account.Properties["token"]);
            };

            context.StartActivity (intent);
            
            return tcs.Task;
        }
		public void Manual_Azure ()
		{
			var a = new WebRedirectAuthenticator (
				initialUrl: new Uri ("https://xamarinauth.azure-mobile.net/login/facebook"),
				redirectUrl: new Uri ("https://xamarinauth.azure-mobile.net/login/done"));

			var vc = a.GetUI ();
			AppDelegate.SharedViewController.PresentViewController (vc, true, null);
			a.Completed += HandleCompleted;
		}
		public void Manual_Azure ()
		{
			var a = new WebRedirectAuthenticator (
				initialUrl: new Uri ("https://xamarinauth.azure-mobile.net/login/facebook"),
				redirectUrl: new Uri ("https://xamarinauth.azure-mobile.net/login/done"));

			var intent = a.GetUI (TestRunner.Shared);
			TestRunner.Shared.StartActivity (intent);
			a.Completed += HandleCompleted;
		}
        public void Manual_Azure()
        {
            var a = new WebRedirectAuthenticator(
                initialUrl: new Uri("https://xamarinauth.azure-mobile.net/login/facebook"),
                redirectUrl: new Uri("https://xamarinauth.azure-mobile.net/login/done"));

            var intent = a.GetUI(TestRunner.Shared);

            TestRunner.Shared.StartActivity(intent);
            a.Completed += HandleCompleted;
        }
示例#5
0
        public void PerformAuthentication(WebRedirectAuthenticator authenticator)
        {
            if (authenticator == null)
            {
                return;
            }

            var authViewController = authenticator.GetUI();

            PresentViewController(authViewController, true, null);
        }
示例#6
0
        protected override void StartLoginUi(object viewReference)
        {
            _authenticator            = new WebRedirectAuthenticator(GetAuthenticationUri(), GetRedirectUri());
            _authenticator.Error     += AuthenticatorOnError;
            _authenticator.Completed += AuthenticatorOnCompleted;

            var activity = (AppCompatActivity)viewReference;
            var intent   = _authenticator.GetUI(activity);

            activity.StartActivity(intent);
        }
        public void Manual_Azure()
        {
            var a = new WebRedirectAuthenticator(
                initialUrl: new Uri("https://xamarinauth.azure-mobile.net/login/facebook"),
                redirectUrl: new Uri("https://xamarinauth.azure-mobile.net/login/done"));

            var vc = a.GetUI();

            AppDelegate.SharedViewController.PresentViewController(vc, true, null);
            a.Completed += HandleCompleted;
        }
        protected override void StartLoginUi(object viewReference)
        {
            _authenticator            = new WebRedirectAuthenticator(GetAuthenticationUri(), GetRedirectUri());
            _authenticator.Error     += AuthenticatorOnError;
            _authenticator.Completed += AuthenticatorOnCompleted;

            _dispatcherHelper.ExecuteOnUiThread(() =>
            {
                _viewController = (UIViewController)viewReference;
                _viewController.PresentViewController(_authenticator.GetUI(), false, null);
            });
        }
        protected override Task <string> LoginAsyncOverride()
        {
            var tcs = new TaskCompletionSource <string>();

            var auth = new WebRedirectAuthenticator(StartUri, EndUri);

            auth.ShowUIErrors            = false;
            auth.ClearCookiesBeforeLogin = false;

            Intent intent = auth.GetUI(this.context);

            auth.Error += (sender, e) =>
            {
                string message = String.Format(CultureInfo.InvariantCulture, Resources.IAuthenticationBroker_AuthenticationFailed, e.Message);
                InvalidOperationException ex = (e.Exception == null)
                    ? new InvalidOperationException(message)
                    : new InvalidOperationException(message, e.Exception);

                tcs.TrySetException(ex);
            };

            auth.Completed += (sender, e) =>
            {
                if (!e.IsAuthenticated)
                {
                    tcs.TrySetException(new InvalidOperationException(Resources.IAuthenticationBroker_AuthenticationCanceled));
                }
                else
                {
                    tcs.TrySetResult(e.Account.Properties["token"]);
                }
            };

            context.StartActivity(intent);

            return(tcs.Task);
        }
        protected override Task <string> LoginAsyncOverride()
        {
            var tcs = new TaskCompletionSource <string>();

            var auth = new WebRedirectAuthenticator(StartUri, EndUri);

            auth.ShowUIErrors            = false;
            auth.ClearCookiesBeforeLogin = false;

            UIViewController c = auth.GetUI();

            UIViewController    controller = null;
            UIPopoverController popover    = null;

            auth.Error += (o, e) =>
            {
                NSAction completed = () =>
                {
                    Exception ex = e.Exception ?? new Exception(e.Message);
                    tcs.TrySetException(ex);
                };

                if (controller != null)
                {
                    controller.DismissViewController(true, completed);
                }
                if (popover != null)
                {
                    popover.Dismiss(true);
                    completed();
                }
            };

            auth.Completed += (o, e) =>
            {
                NSAction completed = () =>
                {
                    if (!e.IsAuthenticated)
                    {
                        tcs.TrySetException(new InvalidOperationException(Resources.IAuthenticationBroker_AuthenticationCanceled));
                    }
                    else
                    {
                        tcs.TrySetResult(e.Account.Properties["token"]);
                    }
                };

                if (controller != null)
                {
                    controller.DismissViewController(true, completed);
                }
                if (popover != null)
                {
                    popover.Dismiss(true);
                    completed();
                }
            };

            controller = view as UIViewController;
            if (controller != null)
            {
                controller.PresentViewController(c, true, null);
            }
            else
            {
                UIView          v         = view as UIView;
                UIBarButtonItem barButton = view as UIBarButtonItem;

                popover = new UIPopoverController(c);

                if (barButton != null)
                {
                    popover.PresentFromBarButtonItem(barButton, UIPopoverArrowDirection.Any, true);
                }
                else
                {
                    popover.PresentFromRect(rect, v, UIPopoverArrowDirection.Any, true);
                }
            }

            return(tcs.Task);
        }
示例#11
0
        /// <summary>
        /// Log a user into a Mobile Services application given a provider name and optional token object.
        /// </summary>
        /// <param name="context" type="Android.Content.Context">
        /// Context used to launch login UI.
        /// </param>
        /// <param name="provider" type="MobileServiceAuthenticationProvider">
        /// Authentication provider to use.
        /// </param>
        /// <param name="token" type="JsonObject">
        /// Optional, provider specific object with existing OAuth token to log in with.
        /// </param>
        /// <returns>
        /// Task that will complete when the user has finished authentication.
        /// </returns>
        internal Task <MobileServiceUser> SendLoginAsync(Context context, MobileServiceAuthenticationProvider provider, JsonObject token = null)
        {
            if (this.LoginInProgress)
            {
                throw new InvalidOperationException(Resources.MobileServiceClient_Login_In_Progress);
            }

            if (!Enum.IsDefined(typeof(MobileServiceAuthenticationProvider), provider))
            {
                throw new ArgumentOutOfRangeException("provider");
            }

            string providerName = provider.ToString().ToLower();

            this.LoginInProgress = true;

            TaskCompletionSource <MobileServiceUser> tcs = new TaskCompletionSource <MobileServiceUser> ();

            if (token != null)
            {
                // Invoke the POST endpoint to exchange provider-specific token for a Windows Azure Mobile Services token

                this.RequestAsync("POST", LoginAsyncUriFragment + "/" + providerName, token)
                .ContinueWith(t =>
                {
                    this.LoginInProgress = false;

                    if (t.IsCanceled)
                    {
                        tcs.SetCanceled();
                    }
                    else if (t.IsFaulted)
                    {
                        tcs.SetException(t.Exception.InnerExceptions);
                    }
                    else
                    {
                        SetupCurrentUser(t.Result);
                        tcs.SetResult(this.CurrentUser);
                    }
                });
            }
            else
            {
                // Launch server side OAuth flow using the GET endpoint

                Uri startUri = new Uri(this.ApplicationUri, LoginAsyncUriFragment + "/" + providerName);
                Uri endUri   = new Uri(this.ApplicationUri, LoginAsyncDoneUriFragment);

                WebRedirectAuthenticator auth = new WebRedirectAuthenticator(startUri, endUri);
                auth.ClearCookiesBeforeLogin = false;
                auth.Error += (o, e) =>
                {
                    this.LoginInProgress = false;

                    Exception ex = e.Exception ?? new Exception(e.Message);
                    tcs.TrySetException(ex);
                };

                auth.Completed += (o, e) =>
                {
                    this.LoginInProgress = false;

                    if (!e.IsAuthenticated)
                    {
                        tcs.TrySetCanceled();
                    }
                    else
                    {
                        SetupCurrentUser(JsonValue.Parse(e.Account.Properties["token"]));
                        tcs.TrySetResult(this.CurrentUser);
                    }
                };

                Intent intent = auth.GetUI(context);
                context.StartActivity(intent);
            }

            return(tcs.Task);
        }
        protected override Task<string> LoginAsyncOverride()
        {
            var tcs = new TaskCompletionSource<string>();

            var auth = new WebRedirectAuthenticator(StartUri, EndUri);
            auth.ShowUIErrors = false;
            auth.ClearCookiesBeforeLogin = false;

            UIViewController c = auth.GetUI();

            UIViewController controller = null;
            UIPopoverController popover = null;

            auth.Error += (o, e) =>
            {
                NSAction completed = () =>
                {
                    Exception ex = e.Exception ?? new Exception(e.Message);
                    tcs.TrySetException(ex);
                };

                if (controller != null)
                    controller.DismissViewController(true, completed);
                if (popover != null)
                {
                    popover.Dismiss(true);
                    completed();
                }
            };

            auth.Completed += (o, e) =>
            {
                NSAction completed = () =>
                {
                    if (!e.IsAuthenticated)
                        tcs.TrySetException(new InvalidOperationException("Authentication was cancelled by the user."));
                    else
                        tcs.TrySetResult(e.Account.Properties["token"]);
                };

                if (controller != null)
                    controller.DismissViewController(true, completed);
                if (popover != null)
                {
                    popover.Dismiss(true);
                    completed();
                }
            };

            controller = view as UIViewController;
            if (controller != null)
            {
                controller.PresentViewController(c, true, null);
            }
            else
            {
                UIView v = view as UIView;
                UIBarButtonItem barButton = view as UIBarButtonItem;

                popover = new UIPopoverController(c);

                if (barButton != null)
                    popover.PresentFromBarButtonItem(barButton, UIPopoverArrowDirection.Any, true);
                else
                    popover.PresentFromRect(rect, v, UIPopoverArrowDirection.Any, true);
            }

            return tcs.Task;
        }
        internal Task<MobileServiceUser> SendLoginAsync(RectangleF rect, object view, MobileServiceAuthenticationProvider provider, JsonObject token = null)
        {
            if (this.LoginInProgress)
            {
                throw new InvalidOperationException(Resources.MobileServiceClient_Login_In_Progress);
            }
            
            if (!Enum.IsDefined(typeof(MobileServiceAuthenticationProvider), provider)) 
            {
                throw new ArgumentOutOfRangeException("provider");
            }

            string providerName = provider.ToString().ToLower();

            this.LoginInProgress = true;

            TaskCompletionSource<MobileServiceUser> tcs = new TaskCompletionSource<MobileServiceUser> ();

            if (token != null)
            {
                // Invoke the POST endpoint to exchange provider-specific token for a Windows Azure Mobile Services token

                this.RequestAsync("POST", LoginAsyncUriFragment + "/" + providerName, token)
                    .ContinueWith (t =>
                    {
                        this.LoginInProgress = false;

                        if (t.IsCanceled)
                            tcs.SetCanceled();
                        else if (t.IsFaulted)
                            tcs.SetException (t.Exception.InnerExceptions);
                        else
                        {
                            SetupCurrentUser (t.Result);
                            tcs.SetResult (this.CurrentUser);
                        }
                    });
            }
            else
            {
                // Launch server side OAuth flow using the GET endpoint

                Uri startUri = new Uri(this.ApplicationUri, LoginAsyncUriFragment + "/" + providerName);
                Uri endUri = new Uri(this.ApplicationUri, LoginAsyncDoneUriFragment);

                WebRedirectAuthenticator auth = new WebRedirectAuthenticator (startUri, endUri);
                auth.ClearCookiesBeforeLogin = false;

                UIViewController c = auth.GetUI();

                UIViewController controller = null;
                UIPopoverController popover = null;

                auth.Error += (o, e) =>
                {
                    this.LoginInProgress = false;

                    if (controller != null)
                        controller.DismissModalViewControllerAnimated (true);
                    if (popover != null)
                        popover.Dismiss (true);

                    Exception ex = e.Exception ?? new Exception (e.Message);
                    tcs.TrySetException (ex);
                };
                
                auth.Completed += (o, e) =>
                {
                    this.LoginInProgress = false;

                    if (controller != null)
                        controller.DismissModalViewControllerAnimated (true);
                    if (popover != null)
                        popover.Dismiss (true);

                    if (!e.IsAuthenticated)
                        tcs.TrySetCanceled();
                    else
                    {
                        SetupCurrentUser (JsonValue.Parse (e.Account.Properties["token"]));
                        tcs.TrySetResult (this.CurrentUser);
                    }
                };

                controller = view as UIViewController;
                if (controller != null)
                {
                    controller.PresentModalViewController (c, true);
                }
                else
                {
                    UIView v = view as UIView;
                    UIBarButtonItem barButton = view as UIBarButtonItem;

                    popover = new UIPopoverController (c);

                    if (barButton != null)
                        popover.PresentFromBarButtonItem (barButton, UIPopoverArrowDirection.Any, true);
                    else
                        popover.PresentFromRect (rect, v, UIPopoverArrowDirection.Any, true);
                }
            }
            
            return tcs.Task;
        }
        internal Task <MobileServiceUser> SendLoginAsync(RectangleF rect, object view, MobileServiceAuthenticationProvider provider, JsonObject token = null)
        {
            if (this.LoginInProgress)
            {
                throw new InvalidOperationException(Resources.MobileServiceClient_Login_In_Progress);
            }

            if (!Enum.IsDefined(typeof(MobileServiceAuthenticationProvider), provider))
            {
                throw new ArgumentOutOfRangeException("provider");
            }

            string providerName = provider.ToString().ToLower();

            this.LoginInProgress = true;

            TaskCompletionSource <MobileServiceUser> tcs = new TaskCompletionSource <MobileServiceUser> ();

            if (token != null)
            {
                // Invoke the POST endpoint to exchange provider-specific token for a Windows Azure Mobile Services token

                this.RequestAsync("POST", LoginAsyncUriFragment + "/" + providerName, token)
                .ContinueWith(t =>
                {
                    this.LoginInProgress = false;

                    if (t.IsCanceled)
                    {
                        tcs.SetCanceled();
                    }
                    else if (t.IsFaulted)
                    {
                        tcs.SetException(t.Exception.InnerExceptions);
                    }
                    else
                    {
                        SetupCurrentUser(t.Result);
                        tcs.SetResult(this.CurrentUser);
                    }
                });
            }
            else
            {
                // Launch server side OAuth flow using the GET endpoint

                Uri startUri = new Uri(this.ApplicationUri, LoginAsyncUriFragment + "/" + providerName);
                Uri endUri   = new Uri(this.ApplicationUri, LoginAsyncDoneUriFragment);

                WebRedirectAuthenticator auth = new WebRedirectAuthenticator(startUri, endUri);
                auth.ClearCookiesBeforeLogin = false;

                UIViewController c = auth.GetUI();

                UIViewController    controller = null;
                UIPopoverController popover    = null;

                auth.Error += (o, e) =>
                {
                    this.LoginInProgress = false;

                    if (controller != null)
                    {
                        controller.DismissModalViewControllerAnimated(true);
                    }
                    if (popover != null)
                    {
                        popover.Dismiss(true);
                    }

                    Exception ex = e.Exception ?? new Exception(e.Message);
                    tcs.TrySetException(ex);
                };

                auth.Completed += (o, e) =>
                {
                    this.LoginInProgress = false;

                    if (controller != null)
                    {
                        controller.DismissModalViewControllerAnimated(true);
                    }
                    if (popover != null)
                    {
                        popover.Dismiss(true);
                    }

                    if (!e.IsAuthenticated)
                    {
                        tcs.TrySetCanceled();
                    }
                    else
                    {
                        SetupCurrentUser(JsonValue.Parse(e.Account.Properties["token"]));
                        tcs.TrySetResult(this.CurrentUser);
                    }
                };

                controller = view as UIViewController;
                if (controller != null)
                {
                    controller.PresentModalViewController(c, true);
                }
                else
                {
                    UIView          v         = view as UIView;
                    UIBarButtonItem barButton = view as UIBarButtonItem;

                    popover = new UIPopoverController(c);

                    if (barButton != null)
                    {
                        popover.PresentFromBarButtonItem(barButton, UIPopoverArrowDirection.Any, true);
                    }
                    else
                    {
                        popover.PresentFromRect(rect, v, UIPopoverArrowDirection.Any, true);
                    }
                }
            }

            return(tcs.Task);
        }
        /// <summary>
        /// Log a user into a Mobile Services application given a provider name and optional token object.
        /// </summary>
        /// <param name="context" type="Android.Content.Context">
        /// Context used to launch login UI.
        /// </param>
        /// <param name="provider" type="MobileServiceAuthenticationProvider">
        /// Authentication provider to use.
        /// </param>
        /// <param name="token" type="JsonObject">
        /// Optional, provider specific object with existing OAuth token to log in with.
        /// </param>
        /// <returns>
        /// Task that will complete when the user has finished authentication.
        /// </returns>
        internal Task<MobileServiceUser> SendLoginAsync(Context context, MobileServiceAuthenticationProvider provider, JsonObject token = null)
        {
            if (this.LoginInProgress)
            {
                throw new InvalidOperationException(Resources.MobileServiceClient_Login_In_Progress);
            }

            if (!Enum.IsDefined(typeof(MobileServiceAuthenticationProvider), provider)) 
            {
                throw new ArgumentOutOfRangeException("provider");
            }

            string providerName = provider.ToString().ToLower();

            this.LoginInProgress = true;

            TaskCompletionSource<MobileServiceUser> tcs = new TaskCompletionSource<MobileServiceUser> ();

            if (token != null)
            {
                // Invoke the POST endpoint to exchange provider-specific token for a Windows Azure Mobile Services token

                this.RequestAsync("POST", LoginAsyncUriFragment + "/" + providerName, token)
                    .ContinueWith (t =>
                    {
                        this.LoginInProgress = false;

                        if (t.IsCanceled)
                            tcs.SetCanceled();
                        else if (t.IsFaulted)
                            tcs.SetException (t.Exception.InnerExceptions);
                        else
                        {
                            SetupCurrentUser (t.Result);
                            tcs.SetResult (this.CurrentUser);
                        }
                    });
            }
            else
            {
                // Launch server side OAuth flow using the GET endpoint

                Uri startUri = new Uri(this.ApplicationUri, LoginAsyncUriFragment + "/" + providerName);
                Uri endUri = new Uri(this.ApplicationUri, LoginAsyncDoneUriFragment);

                WebRedirectAuthenticator auth = new WebRedirectAuthenticator (startUri, endUri);
                auth.ClearCookiesBeforeLogin = false;
                auth.Error += (o, e) =>
                {
                    this.LoginInProgress = false;

                    Exception ex = e.Exception ?? new Exception (e.Message);
                    tcs.TrySetException (ex);
                };
                
                auth.Completed += (o, e) =>
                {
                    this.LoginInProgress = false;

                    if (!e.IsAuthenticated)
                        tcs.TrySetCanceled();
                    else
                    {
                        SetupCurrentUser (JsonValue.Parse (e.Account.Properties["token"]));
                        tcs.TrySetResult (this.CurrentUser);
                    }
                };

                Intent intent = auth.GetUI (context);
                context.StartActivity (intent);
            }

            return tcs.Task;
        }