Advertisement
tasas

Test implementation of FB Auth in C# (not working properly!)

Jun 28th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Text.RegularExpressions;
  14.  
  15. namespace _008_FacebookLogin
  16. {
  17.     /// <summary>
  18.     /// Interaction logic for FBLoginWindow.xaml
  19.     /// </summary>
  20.     public partial class FBLoginWindow : Window
  21.     {
  22.         public FBLoginWindow()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Property to indicate if authentication with facebook was a success
  29.         /// </summary>
  30.         public bool AuthenticatedSuccessfully
  31.         {
  32.             get
  33.             {
  34.                 // Cast to a browser control to get at the current source
  35.                 if (uiFrameLogin.Content.GetType() == typeof(WebBrowser))
  36.                 {
  37.                     WebBrowser webBrowser = (WebBrowser)uiFrameLogin.Content;
  38.                     if (webBrowser.Source != null && webBrowser.Source.ToString().Contains("&error"))
  39.                         return false; // look for an error
  40.                     else
  41.                         if (
  42.                             webBrowser.Source != null &&
  43.                             webBrowser.Source.AbsolutePath.Contains("login_success")
  44.                            )
  45.                         {
  46.                             string temp;
  47.                             temp = Regex.Replace(webBrowser.Source.Fragment, "^.*access_token=", "");
  48.                             Properties.Settings.Default.FBAccessToken = System.Text.RegularExpressions.Regex.Replace(temp, "&.*", "");
  49.  
  50.                             temp = Regex.Replace(webBrowser.Source.Fragment, "^.*access_token=.*&", "");
  51.                             Properties.Settings.Default.FBExpiresIn = System.Text.RegularExpressions.Regex.Replace(temp, "expires_in=", "");
  52.  
  53.                             return true; // if its at this page, we've auth'd successfully
  54.                         }
  55.                 }
  56.  
  57.                 return false; // cant find the success page, cant indicate a successful auth - no return false.
  58.             }
  59.         }
  60.  
  61.  
  62.         /// <summary>
  63.         /// Load the wait page while we load the login page "in the background"
  64.         /// </summary>
  65.         /// <param name="sender"></param>
  66.         /// <param name="e"></param>
  67.         private void Window_Loaded(object sender, RoutedEventArgs e)
  68.         {
  69.             StringBuilder authReqUri = new StringBuilder("https://www.facebook.com/dialog/oauth?client_id=");
  70.             authReqUri.Append(Properties.Settings.Default.FBAppID);
  71.             authReqUri.Append(   "&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=");
  72.             authReqUri.Append(Properties.Settings.Default.FBScope);
  73.             authReqUri.Append("&response_type=token");
  74.             Properties.Settings.Default.FBReqString = authReqUri.ToString();
  75.             return;
  76.         }
  77.  
  78.         /// <summary>
  79.         /// When the window closes, check the login has been successful, if not, by jove, let the user know!
  80.         /// </summary>
  81.         /// <param name="sender"></param>
  82.         /// <param name="e"></param>
  83.         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  84.         {
  85.             if (!AuthenticatedSuccessfully)
  86.             {
  87.                 if (MessageBox.Show("You have not logged in properly, continue to exit?", "Error", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
  88.                 {
  89.                     e.Cancel = true;
  90.                 }
  91.             }
  92.         }
  93.  
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement