public ActionResult Index(string code) { var fb = new FacebookClient(); dynamic result = fb.Post("oauth/access_token", new { client_id = "570964363013096", client_secret = "d6edfe3df4e121a296e8e542a8932154", redirect_uri = RedirectUri.AbsoluteUri, code = code }); var accessToken = result.access_token; //Luu access token ma fb tra ve vao session Session["AccessToken"] = accessToken; FacebookFriendsModel friends = new FacebookFriendsModel(); var client = new FacebookClient(Session["accessToken"].ToString()); dynamic fbresult = client.Get("me/friends"); var data = fbresult["data"].ToString(); friends.friendsListing = JsonConvert.DeserializeObject<List<FacebookFriend>>(data); dbFirstStepDataContext db = new dbFirstStepDataContext(); var item = db.EntityUsers.ToList(); IList<EntityUser> banchung = new List<EntityUser>(); foreach (var ds in friends.friendsListing) { var ban = item.Where(g => g.IdFacebook == ds.id).ToList(); foreach (var item2 in ban) { var daco = db.EntityTheoDois.Where(t => t.IdUser == Convert.ToInt16(Request.Cookies["ftid"].Value) && t.IdBanBe == item2.Id).ToList(); if (daco.Count() == 0) { banchung.Add(item2); EntityTheoDoi dulieu = new EntityTheoDoi(); dulieu.IdUser = Convert.ToInt16(Request.Cookies["ftid"].Value); dulieu.IdBanBe = item2.Id; dulieu.TrangThai = 0; dulieu.Loaifb = 1; db.EntityTheoDois.InsertOnSubmit(dulieu); db.SubmitChanges(); } } } return RedirectToAction("TimBan", "BanBe"); }
// một là thêm bạn mới // follow ban be fb public string Follow(int IdBanBe) { try { int idlogin= Convert.ToInt16(Request.Cookies["ftid"].Value); dbFirstStepDataContext db = new dbFirstStepDataContext(); var user = db.EntityTheoDois.Where(g=>g.IdUser== idlogin&& g.IdBanBe== IdBanBe).FirstOrDefault(); if (user != null) { user.TrangThai = 1; db.SubmitChanges(); } else { EntityTheoDoi theodoi = new EntityTheoDoi(); theodoi.IdUser = idlogin; theodoi.IdBanBe = IdBanBe; theodoi.TrangThai = 1; theodoi.Loaifb = 1; theodoi.Date = DateTime.Now; db.EntityTheoDois.InsertOnSubmit(theodoi); db.SubmitChanges(); } HoatDongModel.AddHoatDong(idlogin, IdBanBe, 1, 0); return "complete"; } catch { return "error"; } }