/// <summary> /// Logs in a <see cref="ParseUser" /> using Facebook for authentication. If a user for the /// given Facebook credentials does not already exist, a new user will be created. /// /// The user will be logged in through Facebook's OAuth web flow using the Windows /// WebAuthenticationBroker. /// </summary> /// <param name="permissions">A list of Facebook permissions to request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The user that was either logged in or created.</returns> public static async Task <ParseUser> LogInAsync(IEnumerable <string> permissions, CancellationToken cancellationToken) { var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); authProvider.Permissions = permissions; authProvider.ResponseUrlOverride = WebAuthenticationBroker.GetCurrentApplicationCallbackUri(); Action <Uri> navigate = async uri => { var result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, uri); if (result.ResponseStatus != WebAuthenticationStatus.Success) { cts.Cancel(); } else { authProvider.HandleNavigation(new Uri(result.ResponseData)); } }; authProvider.Navigate += navigate; try { return(await ParseUser.LogInWithAsync("facebook", cts.Token)); } finally { authProvider.Navigate -= navigate; authProvider.ResponseUrlOverride = null; } }
/// <summary> /// Logs in a <see cref="ParseUser" /> using Facebook for authentication. If a user for the /// given Facebook credentials does not already exist, a new user will be created. /// </summary> /// <param name="facebookId">The user's Facebook ID.</param> /// <param name="accessToken">A valid access token for the user.</param> /// <param name="expiration">The expiration date of the access token.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The user that was either logged in or created.</returns> public static Task <ParseUser> LogInAsync(string facebookId, string accessToken, DateTime expiration, CancellationToken cancellationToken) { return(ParseUser.LogInWithAsync("facebook", authProvider.GetAuthData(facebookId, accessToken, expiration), cancellationToken)); }
/// <summary> /// Logs in a <see cref="ParseUser" /> using Facebook for authentication. If a user for the /// given Facebook credentials does not already exist, a new user will be created. /// /// The user will be logged in through Facebook's OAuth web flow, so you must supply a /// <paramref name="webView"/> that will be navigated to Facebook's authentication pages. /// </summary> /// <param name="webView">A web view that will be used to present the authorization pages /// to the user.</param> /// <param name="permissions">A list of Facebook permissions to request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The user that was either logged in or created.</returns> public static async Task <ParseUser> LogInAsync(WebBrowser webView, IEnumerable <string> permissions, CancellationToken cancellationToken) { authProvider.Permissions = permissions; LoadCompletedEventHandler loadCompleted = (_, e) => authProvider.HandleNavigation(e.Uri); webView.LoadCompleted += loadCompleted; Action <Uri> navigate = uri => webView.Navigate(uri); authProvider.Navigate += navigate; var result = await ParseUser.LogInWithAsync("facebook", cancellationToken); authProvider.Navigate -= navigate; webView.LoadCompleted -= loadCompleted; return(result); }
/// <summary> /// Call this method within your RootFrame.Navigating event handler to complete native Facebook /// sign-on. When handling a Facebook login redirect URI, this method will cancel the /// pending navigation, begin asynchronously logging in the user, and immediately navigate /// to the <paramref name="redirectUri"/>. /// /// Your code will usually look like this: /// <code> /// RootFrame.Navigating += async (sender, e) => { /// if (ParseFacebookUtils.IsLoginRedirect(e.Uri)) { /// ParseUser user = await ParseFacebookUtils.EndLoginAsync( /// sender, e, new Uri("/LandingPage.xaml", UriKind.Relative)); /// // A new user is now logged in. /// } /// }; /// </code> /// </summary> /// <param name="sender">The sender for the Navigating event.</param> /// <param name="e">The Navigating event args.</param> /// <param name="redirectUri">The Uri within your app to redirect to.</param> /// <returns>The ParseUser created or logged in using Facebook credentials, or null if /// this was not a Facebook login redirect.</returns> public async static Task <ParseUser> EndLogInAsync(object sender, NavigatingCancelEventArgs e, Uri redirectUri) { if (!IsLogInRedirect(e.Uri)) { return(null); } authProvider.ResponseUrlOverride = NativeResponseUrl; Action <Uri> navigate = (_) => { }; authProvider.Navigate += navigate; try { var cts = new CancellationTokenSource(); // Kicks off a dummy to restart authentication. var result = ParseUser.LogInWithAsync("facebook", cts.Token); // Complete the authentication. var uri = new Uri(new Uri("dummy:///"), e.Uri); var queryString = ParseClient.DecodeQueryString(uri.Query.Substring(1)); var launchUri = new Uri(Uri.UnescapeDataString(queryString["encodedLaunchUri"])); if (!authProvider.HandleNavigation(launchUri)) { // Cancel the pending attempt to log in if for some reason this wasn't actually // a facebook login navigation (unlikely at this point) cts.Cancel(); } // Cancel navigation and redirect to the new Uri. e.Cancel = true; var navService = sender as NavigationService; if (navService == null) { throw new ArgumentException("sender must be a NavigationService", "sender"); } // Welcome to Windows Phone... sometimes you just have to dispatch for no // particularly good reason. Deployment.Current.Dispatcher.BeginInvoke(() => navService.Navigate(redirectUri)); return(await result); } finally { authProvider.Navigate -= navigate; authProvider.ResponseUrlOverride = null; } }