示例#1
0
        public ActionResult FacebookCallback(string code)
        {
            var CurrentUser = new User();
            var fb          = new FacebookClient();
            var ctx         = new CityGuideContext();
            var db          = new CityGuideDatabase(ctx);

            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id     = "1018612288230748",
                client_secret = "85a5bdf69dda687274a5be028818f3a3",
                redirect_uri  = RedirectUri.AbsoluteUri,
                code          = code
            });

            var accessToken = result.access_token;

            // Store the access token in the session for farther use
            Session["AccessToken"] = accessToken;

            // update the facebook client with the access token so
            // we can make requests on behalf of the user
            fb.AccessToken = accessToken;

            // Get the user's information
            dynamic me = fb.Get("me?fields=first_name,middle_name,last_name,id,email");

            CurrentUser.Email      = me.email;
            CurrentUser.FirstName  = me.first_name;
            CurrentUser.MiddleName = me.middle_name;
            CurrentUser.LastName   = me.last_name;
            CurrentUser.FacebookID = me.id;

            Session["FacebookID"] = CurrentUser.FacebookID;

            db.AddUserToDB(CurrentUser);

            // Set the auth cookie
            FormsAuthentication.SetAuthCookie(CurrentUser.Email, false);

            if (Request.QueryString["ReturnURL"] != null)
            {
                return(Redirect(Request.QueryString["ReturnURL"]));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }