PageRenderTime 41ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 15ms app.codeStats 1ms

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