public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInHelper.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public async Task<ActionResult> Dummy()
        {
            const string userName = "******";
            const string firstName = "Xuan";
            const string lastName = "Tran";
            const string profile = "profile";
            const string cover = "cover";
            for (var i = 0; i < 100; i++)
            {
                var user = new ApplicationUser
                {
                    UserName = new StringBuilder().Append(userName).Append(i).ToString(),
                    Email = new StringBuilder().Append(userName).Append(i).Append("@gmail.com").ToString(),
                    PhoneNumber =
                        new StringBuilder().Append(MyConstants.nullDefault)
                            .Append(Guid.NewGuid())
                            .Append("@null.null")
                            .ToString(),
                    FirstName = new StringBuilder().Append(firstName).Append(i).ToString(),
                    LastName = new StringBuilder().Append(lastName).Append(i).ToString(),
                    Created = DateTime.Now.ToString(),
                    Modified = DateTime.Now.ToString(),
                    Personal = true,
                    EmailConfirmed = true,
                    ProfilePictureSource =
                        new StringBuilder().Append(profile).Append(" (" + i + ")").Append(".jpg").ToString(),
                    ProfilePicture =
                        new StringBuilder().Append(profile).Append(" (" + i + ")").Append(".jpg").ToString(),
                    CoverPicture = new StringBuilder().Append(cover).Append(" (" + i + ")").Append(".jpg").ToString(),
                    CoverPictureSource =
                        new StringBuilder().Append(cover).Append(" (" + i + ")").Append(".jpg").ToString()
                };
                if (i <= 25)
                {
                    user.City = "Ha Noi";
                    user.Country = "Viet Nam";
                }
                if (i>25 && i <= 50)
                {
                    user.City = "Shanghai";
                    user.Country = "China";
                }
                if (i>50 &&i <= 75)
                {
                    user.City = "Bac Kinh";
                    user.Country = "China";
                }
                if (i > 75)
                {
                    user.City = "Tokyo";
                    user.Country = "Japan";
                }
                var password = new StringBuilder().Append("kh0ngbiet").Append("Xuan@6288").ToString();
                var result = await UserManager.CreateAsync(user, password);
                var repo = new Repository<ShareSettingModel>(DbCollection.ShareSetting);
                repo.Insert(new ShareSettingModel {UserId = user.Id});

                var repoEduction = new Repository<EducationModel>(DbCollection.Education);
                // Create education
                if (i <= 25)
                {
                    repoEduction.Insert(new EducationModel
                    {
                        UserId = user.Id,
                        SchoolName = "NIIT",
                    });
                }
                if (i > 25 && i <= 50)
                {
                    repoEduction.Insert(new EducationModel
                    {
                        UserId = user.Id,
                        SchoolName = "Aptech",
                    });
                }
                if (i>50 && i <= 75)
                {
                    repoEduction.Insert(new EducationModel
                    {
                        UserId = user.Id,
                        SchoolName = "Bách Khoa",
                    });
                }
                if (i>75)
                {
                    repoEduction.Insert(new EducationModel
                    {
                        UserId = user.Id,
                        SchoolName = "Hoa Sen",
                    });
                }
            }
            return RedirectToAction("Index", "Home");
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid) return View();
            // Check exist
            if (CheckExistAccount(model.InputSource))
            {
                ModelState.AddModelError("InputSource", "Account had been used.");
                // If we got this far, something failed, redisplay form
                return View();
            }

            // Custom validate email, phone
            const string emailRegex = @"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$";
            const string phoneRegex = @"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
            var mailRe = new Regex(emailRegex);
            var phoneRe = new Regex(phoneRegex);
            if (mailRe.IsMatch(model.InputSource))
            {
                model.Email = model.InputSource;
            }
            else if (phoneRe.IsMatch(model.InputSource))
            {
                model.Phone = model.InputSource;
            }
            else
            {
                model.UserName = model.InputSource;
            }

            if (string.IsNullOrEmpty(model.Email))
            {
                model.Email = MyConstants.nullDefault + Guid.NewGuid().ToString() + "@null.null";
            }
            if (string.IsNullOrEmpty(model.UserName))
            {
                model.UserName = MyConstants.nullDefault + Guid.NewGuid().ToString();
            }
            if (string.IsNullOrEmpty(model.Phone))
            {
                model.Phone = MyConstants.nullDefault + Guid.NewGuid().ToString() + "@null.null";
            }
            var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, PhoneNumber = model.Phone, FullName = model.FullName, Created = DateTime.Now.ToString(), Modified = DateTime.Now.ToString(), Personal = true};
            //var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Created = DateTime.Now, Modified = DateTime.Now};
            // Store password for Xuan

            // End
            model.Password = model.Password + "Xuan@6288";
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                // INSERT Share db
                var repo = new Repository<ShareSettingModel>(DbCollection.ShareSetting);
                repo.Insert(new ShareSettingModel{ UserId = user.Id });
                
                //if (!model.Email.Contains(MyConstants.nullDefault))
                //{
                //    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //    //string bodyEmail = "Please confirm your account by clicking this link: <a href=\"" + callbackUrl +
                //    //                   "\">link</a>";
                //    var modelEmail = new EmailTemplateViewModel
                //    {
                //        Link = callbackUrl
                //    };
                //    var bodyEmail = RenderRazorViewToString("TemplateContact", modelEmail);
                //    await UserManager.SendEmailAsync(user.Id, "Confirm your account", bodyEmail);
                //    ViewBag.Link = callbackUrl;
                //}
                    
                await SignInHelper.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                // Alert sucess
                if (!model.UserName.Contains(MyConstants.nullDefault))
                {
                    Session["register-info"] = model.UserName;
                }
                else if (!model.Email.Contains(MyConstants.nullDefault))
                {
                    Session["register-info"] = model.Email;
                }
                else
                {
                    Session["register-info"] = model.Phone;
                }
                //Session["register-info"] = model.Email;
                return RedirectToAction("Index", "Home");
            }

            AddErrors(result, "register");

            // If we got this far, something failed, redisplay form
            return View();
        }