示例#1
0
        /// <summary>
        /// Begins a server-side authentication flow by navigating the WebAuthenticationBroker
        /// to the <paramref name="startUrl"/>.
        /// </summary>
        /// <param name="startUrl">The URL that the browser-based control should
        /// first navigate to in order to start the authenication flow.
        /// </param>
        /// <param name="endUrl">The URL that indicates the authentication flow has
        /// completed. Upon being redirected to any URL that starts with the
        /// <paramref name="endUrl"/>, the browser-based control must stop navigating and
        /// return the response data to the <see cref="AuthenticationBroker"/>.
        /// </param>
        /// <param name="loginView">If this control is specified it will be used to do the login,
        /// otherwise navigation to a page containing a LoginView will occur.</param>
        /// <returns>
        /// The object containing the user profile, JSON Web Token signed and the access_token.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Thrown if the user cancels the authentication flow or an error occurs during
        /// the authentication flow.
        /// </exception>
        public Task <Auth0User> AuthenticateAsync(Uri startUrl, Uri endUrl, LoginView loginView = null)
        {
            this.StartUri = startUrl;
            this.EndUri   = endUrl;

            this.AuthenticationInProgress = true;

            if (loginView == null)
            {
                PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;

                if (rootFrame == null)
                {
                    throw new InvalidOperationException();
                }

                //hook up the broker to the page on the event.
                rootFrame.Navigated += rootFrame_Navigated;

                // Navigate to the login page.
                rootFrame.Navigate(this.LoginPageUri);
            }
            else
            {
                loginView.Broker = this;
                loginView.StartLogin();
            }

            Task <Auth0User> task = Task <Auth0User> .Factory.StartNew(() =>
            {
                authenticateFinishedEvent.WaitOne();
                if (this.responseStatus != PhoneAuthenticationStatus.Success)
                {
                    string message;
                    if (this.responseStatus == PhoneAuthenticationStatus.UserCancel)
                    {
                        throw new AuthenticationCancelException();
                    }
                    else
                    {
                        message = string.Format(CultureInfo.InvariantCulture,
                                                "Authentication has failed. {0}",
                                                this.responseErrorDetail);
                    }

                    throw new AuthenticationErrorException(message);
                }

                return(GetTokenStringFromResponseData(this.responseData));
            });

            return(task);
        }
        /// <summary>
        /// Initiates the authentication operation by pointing the browser control
        /// to the PhoneWebAuthenticationBroker.StartUri.  If the PhoneWebAuthenticationBroker
        /// isn't currently in the middle of an authentication operation, then we immediately
        /// navigate back.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Make sure that there is an authentication operation in progress.
            // If not, we'll navigate back to the previous page.
            if (!Broker.AuthenticationInProgress)
            {
                this.NavigationService.GoBack();
            }

            if (!authenticationStarted)
            {
                authenticationStarted = true;

                // Point the browser control to the authentication start page.
                LoginView.Broker            = Broker;
                LoginView.NavigationService = NavigationService;
                LoginView.StartLogin();
            }
        }