Ejemplo n.º 1
0
        public async Task <ActionResult> FacebookLogin(string token)
        {
            WebClient client     = new WebClient();
            string    JsonResult = client.DownloadString(string.Concat(
                                                             "https://graph.facebook.com/me?access_token=", token));

            JObject jsonUserInfo = JObject.Parse(JsonResult);
            string  id           = jsonUserInfo.Value <string>("id");
            string  nameonFb     = jsonUserInfo.Value <string>("name");

            // store user's information here...
            var getUserFacebook = await db.UserLogins.FirstOrDefaultAsync(u => u.IdFacebook == id);

            if (getUserFacebook != null)
            {
                if (getUserFacebook.AvatarSyn == true) // cập nhật avatar tự động từ facebook
                {
                    // update avatar
                    await AppHelper.AvatarSyn(getUserFacebook.IdFacebook);
                }
                // login\
                var user = await UserManager.FindAsync(getUserFacebook.UserNameCopy, "cungphim.com@9999");
                await SignInAsync(user, false);

                AppHelper.SetCookieOfFace();
                return(RedirectToAction("", "myprofile")); // Returun URL
            }
            else
            {
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    UserName = AppHelper.ConvertToNonUnicode(nameonFb.RemoveSpecialString().Replace(" ", "")), Token = token
                }));
            }

            //return View();
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Manage"));
            }

            if (ModelState.IsValid)
            {
                WebClient client     = new WebClient();
                string    JsonResult = client.DownloadString(string.Concat(
                                                                 "https://graph.facebook.com/me?access_token=", model.Token));
                // Json.Net is really helpful if you have to deal
                // with Json from .Net http://json.codeplex.com/
                JObject jsonUserInfo = JObject.Parse(JsonResult);
                // you can get more user's info here. Please refer to:
                //     http://developers.facebook.com/docs/reference/api/user/
                string name  = jsonUserInfo.Value <string>("name");
                string email = jsonUserInfo.Value <string>("email");
                //string locale = jsonUserInfo.Value<string>("locale");
                //string facebook_userID = jsonUserInfo.Value<string>("id");
                string id = jsonUserInfo.Value <string>("id");

                // Get the information about the user from the external login provider
                var user = new ApplicationUser()
                {
                    UserName     = model.UserName,
                    PasswordHash = "cungphim.com@9999",
                };
                user.UserExtentLogin = new UserExtentLogin {
                    Email = email, KeyLogin = user.Id, CreatedDate = DateTime.Now, FullName = name, Verify = Verify.NO, UserNameCopy = model.UserName, IdFacebook = id, AvatarSyn = true
                };


                // check email
                if (!string.IsNullOrEmpty(email))
                {
                    var checkEmail = await db.UserLogins.FirstOrDefaultAsync(ul => ul.Email == email);

                    if (checkEmail != null)
                    {
                        OutputErrors("Email đã tồn tại trong hệ thống");
                    }
                }
                else
                {
                    var result = await UserManager.CreateAsync(user, user.PasswordHash);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent : false);

                        //send mail
                        // download avatar
                        await AppHelper.AvatarSyn(id);

                        AppHelper.SetCookieOfFace();
                        return(RedirectToAction("Index", "MyProfile"));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }