Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.Text.RegularExpressions;
- namespace _008_FacebookLogin
- {
- /// <summary>
- /// Interaction logic for FBLoginWindow.xaml
- /// </summary>
- public partial class FBLoginWindow : Window
- {
- public FBLoginWindow()
- {
- InitializeComponent();
- }
- /// <summary>
- /// Property to indicate if authentication with facebook was a success
- /// </summary>
- public bool AuthenticatedSuccessfully
- {
- get
- {
- // Cast to a browser control to get at the current source
- if (uiFrameLogin.Content.GetType() == typeof(WebBrowser))
- {
- WebBrowser webBrowser = (WebBrowser)uiFrameLogin.Content;
- if (webBrowser.Source != null && webBrowser.Source.ToString().Contains("&error"))
- return false; // look for an error
- else
- if (
- webBrowser.Source != null &&
- webBrowser.Source.AbsolutePath.Contains("login_success")
- )
- {
- string temp;
- temp = Regex.Replace(webBrowser.Source.Fragment, "^.*access_token=", "");
- Properties.Settings.Default.FBAccessToken = System.Text.RegularExpressions.Regex.Replace(temp, "&.*", "");
- temp = Regex.Replace(webBrowser.Source.Fragment, "^.*access_token=.*&", "");
- Properties.Settings.Default.FBExpiresIn = System.Text.RegularExpressions.Regex.Replace(temp, "expires_in=", "");
- return true; // if its at this page, we've auth'd successfully
- }
- }
- return false; // cant find the success page, cant indicate a successful auth - no return false.
- }
- }
- /// <summary>
- /// Load the wait page while we load the login page "in the background"
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- StringBuilder authReqUri = new StringBuilder("https://www.facebook.com/dialog/oauth?client_id=");
- authReqUri.Append(Properties.Settings.Default.FBAppID);
- authReqUri.Append( "&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=");
- authReqUri.Append(Properties.Settings.Default.FBScope);
- authReqUri.Append("&response_type=token");
- Properties.Settings.Default.FBReqString = authReqUri.ToString();
- return;
- }
- /// <summary>
- /// When the window closes, check the login has been successful, if not, by jove, let the user know!
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (!AuthenticatedSuccessfully)
- {
- if (MessageBox.Show("You have not logged in properly, continue to exit?", "Error", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
- {
- e.Cancel = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement