Advertisement
Guest User

Facebook Registration C# handling

a guest
Dec 31st, 2010
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. // In Default.aspx:
  2.  
  3. <iframe src="http://www.facebook.com/plugins/registration.php?
  4.             client_id=your_client_id&
  5.             redirect_uri=http%3A%2F%2Fmydomain.com%2FCallback.aspx&
  6.             fields=name,birthday,gender,location,email"
  7.         scrolling="auto"
  8.         frameborder="no"
  9.         style="border:none"
  10.         allowTransparency="true"
  11.         width="100%"
  12.         height="330">
  13. </iframe>
  14.  
  15. // In Callback.aspx.cs:
  16.  
  17. using System;
  18. using Newtonsoft.Json.Linq;
  19. using System.Text;
  20. using System.Security.Cryptography;
  21.  
  22. public partial class Callback: System.Web.UI.Page
  23. {
  24.     protected void Page_Load(object sender, EventArgs e)
  25.     {
  26.         var signed_request = Request.Form["signed_request"];
  27.  
  28.         var obj = ParseSignedRequest(signed_request, "your_secret_key");
  29.  
  30.         var name = obj["name"].Value<string>();
  31.         var birthday = obj["birthday"].Value<DateTime>();
  32.         var location = obj["location"]["name"].Value<string>();
  33.         var gender = obj["gender"].Value<string>();
  34.         var email = obj["gender"].Value<string>();
  35.  
  36.         // Save values to database...
  37.  
  38.     }
  39.  
  40.     private JToken ParseSignedRequest(string signedRequestValue, string secret_key)
  41.     {
  42.         string[] signedRequestSplit = signedRequestValue.Split('.');
  43.         string expectedSignature = signedRequestSplit[0];
  44.         string payload = signedRequestSplit[1];
  45.  
  46.         // Attempt to get same hash
  47.         var encoding = new UTF8Encoding();
  48.         var hmac = SignWithHmac(encoding.GetBytes(payload), encoding.GetBytes(secret_key));
  49.         var hmacBase64 = ToUrlBase64String(hmac);
  50.  
  51.         if (hmacBase64 != ReplaceSpecialCharactersInSignedRequest(expectedSignature))
  52.             return null;
  53.  
  54.         var decodedJson = ReplaceSpecialCharactersInSignedRequest(payload);
  55.         var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
  56.  
  57.  
  58.         var data = JObject.Parse(encoding.GetString(base64JsonArray));
  59.  
  60.         if (data.HasValues && data["registration"] != null)
  61.         {
  62.             return data["registration"];
  63.         }
  64.         else
  65.             return null;
  66.  
  67.     }
  68.  
  69.     private static string ReplaceSpecialCharactersInSignedRequest(string str)
  70.     {
  71.         return str.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
  72.     }
  73.  
  74.     private static byte[] SignWithHmac(byte[] dataToSign, byte[] keyBody)
  75.     {
  76.         using (var hmacAlgorithm = new HMACSHA256(keyBody))
  77.         {
  78.             hmacAlgorithm.ComputeHash(dataToSign);
  79.             return hmacAlgorithm.Hash;
  80.         }
  81.     }
  82.     private static string ToUrlBase64String(byte[] Input)
  83.     {
  84.         return ReplaceSpecialCharactersInSignedRequest(Convert.ToBase64String(Input));
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement