Esempio n. 1
0
        public static bool TryGet(long facebookId, out FacebookUser user)
        {
            user = users.SingleOrDefault(u => u.FacebookId == facebookId);
            if (user == null)
                user = db.FacebookUsers.SingleOrDefault(u => u.FacebookId == facebookId);

            if (user == null)
                return false;
            return true;
        }
Esempio n. 2
0
        public ActionResult OAuth(string code)
        {
            var client_id = "385275068172573";
            var client_secret = "de6ad5406db6e07180795a4be2b10926";

            var link = "https://graph.facebook.com/oauth/access_token?"
                + "client_id=" + client_id
                + "&redirect_uri=" + redirect_uri
                + "&client_secret=" + client_secret
                + "&code=" + code;

            //Fetch access token
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(link);
            WebResponse response = request.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream());
            string data = stream.ReadToEnd();
            stream.Close();

            //Extract access token and expiration date from result using regexp
            var resultGroups = Regex.Match(data, "access_token=(.+?)&expires=(.+)").Groups;

            var accessToken = resultGroups[1].Value;
            var expires = resultGroups[2].Value;

            //Create a client
            FacebookClient client = new FacebookClient(accessToken);

            //Fetch user information and facebook id
            dynamic me = client.Get("me?fields=" + FacebookUser.Fields);
            long facebookId = Convert.ToInt64(me.id);

            FacebookUser user = new FacebookUser
            {
                AccessToken = client.AccessToken,
                Expires = DateTimeConvertor.FromUnixTime(expires),
                FacebookId = facebookId,
                Name = (string)me.name,
                Picture = (string)me.picture
            };

            //Save the user in the database
            UserStore.Add(user);

            //Save in session
            Session["FacebookUser"] = user;

            //Store id in a cookie
            FormsAuthentication.SetAuthCookie(facebookId.ToString(), false);

            //Redirect to first page
            return RedirectToAction("Index", "Home");
        }
Esempio n. 3
0
        public static void Add(FacebookUser user)
        {
            if (users.SingleOrDefault(u => u.FacebookId == user.FacebookId) == null)
            {
                users.Add(user);
            }

            if (db.FacebookUsers.SingleOrDefault(x => x.FacebookId == user.FacebookId) == null)
            {
                db.FacebookUsers.Add(user);
                db.SaveChanges();
            }
        }
Esempio n. 4
0
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            long facebookId;
            //Get the user id from cookie
            if (long.TryParse(User.Identity.Name, out facebookId))
            {
                FacebookUser user;

                //Check if the user is in the database
                if (UserStore.TryGet(facebookId, out user))
                {
                    //use the saved accesstoken
                    FacebookClient = new FacebookClient(user.AccessToken);

                    //Make sure that we still can use the accesstoken (it might have expired)
                    try
                    {
                        dynamic result = FacebookClient.Get("me?fields=" + FacebookUser.Fields); //will throw into catch if the access token has expired

                        FacebookUser = (FacebookUser)JsonBinder.ParseJson(typeof(FacebookUser), result);
                        Session["FacebookUser"] = FacebookUser;

                        EdmPortalenDb db = new EdmPortalenDb();
                        user.LastSeen = DateTime.Now;
                        db.Entry(user).State = System.Data.EntityState.Modified;
                        db.SaveChanges();
                    }
                    catch (FacebookOAuthException ex)
                    {
                        //User will be redirected
                    }
                }
            }

            if (FacebookUser == null)
            {
                var redirect = RedirectToAction("LogOn", "Account");
                filterContext.Result = redirect;
            }

            base.OnAuthorization(filterContext);
        }