コード例 #1
0
ファイル: LoginController.cs プロジェクト: ogggy4858/Video
        public ActionResult FacebookCallback(string code)
        {
            PostCategory2Controller.se = null;
            var     fb     = new FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id     = ConfigurationManager.AppSettings["FbAppId"],
                client_secret = ConfigurationManager.AppSettings["FbAppSecret"],
                redirect_uri  = RedirectUri.AbsoluteUri,
                code          = code
            });
            var accessToken = result.access_token;

            if (!string.IsNullOrEmpty(accessToken))
            {
                fb.AccessToken = accessToken;
                dynamic me         = fb.Get("me?fields=first_name,middle_name,last_name,id,email");
                string  email      = me.email;
                string  userName   = me.email;
                string  firstname  = me.first_name;
                string  middlename = me.middle_name;
                string  lastname   = me.last_name;

                Person p = new Person();
                p.Email      = email;
                p.FullName   = firstname + " " + middlename + " " + lastname;
                p.Status     = true;
                p.CreateDate = DateTime.Now;
                p.PositionID = 4;
                var num = dao.AddForFacebook(p);

                if (num == 1)
                {
                    CheckInUser(p.Email, p.FullName, p.ID);
                    History h = new History();
                    InsertHistory(h, p);
                }
                else if (num == 0)
                {
                    var ob = user.ViewDetails(email);
                    CheckInUser(ob.Email, ob.FullName, ob.ID);
                    History h = new History();
                    InsertHistory(h, ob);
                }
                else if (num == -1)
                {
                    ModelState.AddModelError("", "Khong the dang nhap");
                }
            }
            else
            {
            }
            return(RedirectToAction("Index", "Home"));
        }