PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/FB/Controllers/HomeController.cs

https://github.com/mikebastarache/AspNet_Mvc_Facebook_Sample
C# | 128 lines | 90 code | 22 blank | 16 comment | 11 complexity | 2d3323ab4e82d4e480b25163628d1af2 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Facebook;
  7. using System.Threading;
  8. using System.Globalization;
  9. using System.Text;
  10. using Newtonsoft.Json.Linq;
  11. using System.Dynamic;
  12. namespace FB.Controllers
  13. {
  14. public class HomeController : Controller
  15. {
  16. public string AppId = "374918325879397";
  17. public string AppSecret = "54c997ce7eb78b85f906db62e758e368";
  18. public string local_url = "http://fb.local";
  19. public string redirect_uri = "http://www.facebook.com/mmdevel/app_374918325879397";
  20. public string ExtendedPermissions = "";
  21. public int ContestID = 7;
  22. public string lang = "en_US";
  23. //
  24. // GET: /Home/
  25. public ActionResult Index()
  26. {
  27. //GETS SIGNED REQUEST FROM FACEBOOK WHEN APPLICATION IS LOADED
  28. if (Request.Params["signed_request"] != null)
  29. {
  30. string payload = Request.Params["signed_request"].Split('.')[1];
  31. //STORE A STRING VERSION OF SIGNED REQUEST INTO A SESSION VARIABLE SO APPLICATION CAN USE DATA
  32. Session["signed_request"] = payload;
  33. var encoding = new UTF8Encoding();
  34. var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
  35. var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
  36. var json = encoding.GetString(base64JsonArray);
  37. var o = JObject.Parse(json);
  38. string PageID = Convert.ToString(o.SelectToken("page.id")).Replace("\"", "");
  39. string oauth_token = Convert.ToString(o.SelectToken("oauth_token")).Replace("\"", "");
  40. string algorithm = Convert.ToString(o.SelectToken("algorithm")).Replace("\"", "");
  41. string PageLiked = Convert.ToString(o.SelectToken("page.liked")).Replace("\"", "");
  42. string fbuid = Convert.ToString(o.SelectToken("user_id")).Replace("\"", "");
  43. string Country = Convert.ToString(o.SelectToken("user.country")).Replace("\"", "");
  44. string locale = Convert.ToString(o.SelectToken("user.locale")).Replace("\"", "");
  45. string lang = "en-CA";
  46. if (locale.Substring(0, 2) == "fr")
  47. {
  48. lang = "fr-CA";
  49. }
  50. Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
  51. Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
  52. //SET USER ID
  53. Session["fbuid"] = fbuid;
  54. Session["lang"] = lang;
  55. //IF USER LIKES THE PAGE, THEN SEE IF THEY ARE AUTHENTICATED WITH THE APPLICATION
  56. if (PageLiked == "True")
  57. {
  58. //IF SIGNED REQUEST HAS NO ACCESS TOKEN, THEN APPLICATION IS NOT AUTHENTICATED
  59. if (oauth_token == "")
  60. {
  61. dynamic parameters = new ExpandoObject();
  62. parameters.client_id = AppId;
  63. parameters.client_secret = AppSecret;
  64. parameters.redirect_uri = local_url;
  65. // The requested response: an access token (token), an authorization code (code), or both (code token).
  66. parameters.response_type = "token";
  67. // add the 'scope' parameter only if we have extendedPermissions.
  68. if (!string.IsNullOrWhiteSpace(ExtendedPermissions))
  69. parameters.scope = ExtendedPermissions;
  70. // generate the login url
  71. var fb = new FacebookClient();
  72. var loginUrl = fb.GetLoginUrl(parameters);
  73. string oauth_url = "https://www.facebook.com/dialog/oauth/?client_id=" + AppId + "&redirect_uri=" + local_url + "&scope=" + ExtendedPermissions;
  74. Response.Write("<script>");
  75. Response.Write("var oauth_url = '" + oauth_url + "';");
  76. Response.Write("window.top.location = oauth_url;");
  77. Response.Write("</script>");
  78. }
  79. else
  80. {
  81. //IF SIGNED REQUEST HAS AN AUTHENTICATED TOKEN, THEN PROCEED WITH THE APPLICATION
  82. //CHECK TO SEE IF THE USER HAS ENTERED THE CONTEST
  83. ViewBag.x = "SIGNED REQUEST IS AUTHENTICATED!!!!";
  84. ViewBag.PageID = PageID;
  85. ViewBag.oauth_token = oauth_token;
  86. ViewBag.algorithm = algorithm;
  87. ViewBag.PageLiked = PageLiked;
  88. ViewBag.fbuid = fbuid;
  89. ViewBag.Country = Country;
  90. ViewBag.locale = locale;
  91. ViewBag.lang = lang;
  92. }
  93. }
  94. //END PAGE LIKE CHECK
  95. }
  96. //END SIGNED REQUEST CHECK
  97. //NO SIGNED REQUEST AVAILABLE BECAUSE FACEBOOK AUTHENTICATED REDIRECTED USE TO WEB HOST
  98. //THIS WILL HANDLE THE RE-DIRECT FROM THE WEB HOST BACK TO FACEBOOK AFTER THE USER AUTHENTICATES THE APPLICATION.
  99. if (Request.Params["code"] != null)
  100. {
  101. Response.Redirect(redirect_uri, true);
  102. }
  103. return View();
  104. }
  105. }
  106. }