public ActionResult Register(RegisterViewModel model)
        {
            if (DefaultView.GetRandomCapcha() != model.CaptchaCode)
            {
                ViewBag.message = "Mã xác minh không đúng.";
                return(View());
            }
            if (model.ConfirmPassword != model.Password)
            {
                ViewBag.message = "Mật khẩu xác nhận không giống.";
                return(View());
            }
            //get internal user with email & code
            IDbConnection dbConn = new OrmliteConnection().openConn();
            var           user   = AuthUser.GetByCode(model.Email, LoginType.InternalLogin, null, false);

            if (user != null)
            {
                ViewBag.message = "Tài khoản đã tồn tại";
                return(View());
            }
            //create user internal
            user          = new AuthUser();
            user.password = SqlHelper.GetMd5Hash(model.Password);

            user.createdat     = DateTime.Now;
            user.updatedat     = DateTime.Now;
            user.entrycode     = model.Email;
            user.entryname     = model.Email;
            user.fullname      = model.Email;
            user.email         = model.Email;
            user.logintype     = (int)LoginType.InternalLogin;
            user.loginprovider = "";
            user.isactive      = true;
            user.entryid       = user.AddOrUpdate(user.entryid);

            var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Name, user.fullname),
                new Claim(ClaimTypes.Email, string.IsNullOrEmpty(user.email)?"":user.email),
                new Claim(ClaimTypes.PrimarySid, user.entryid.ToString())
            },
                                              "ApplicationCookie");

            var ctx         = Request.GetOwinContext();
            var authManager = ctx.Authentication;

            authManager.SignIn(identity);

            return(RedirectToAction("Index", "Home"));
        }
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LogOnModel model, string returnUrl)
        {
            returnUrl = string.IsNullOrEmpty(returnUrl) ? "" : returnUrl;

            if (DefaultView.GetRandomCapcha() != model.CaptchaCode)
            {
                ViewBag.message = "Mã xác minh không đúng.";
                return(View());
            }


            IDbConnection dbConn = new OrmliteConnection().openConn();
            var           user   = AuthUser.GetByCode(model.UserName, null, false);

            if (user != null && new AccountMembershipService().ValidateUser(model.UserName, model.Password))
            {
                //FormsAuthentication.SetAuthCookie(model.UserName, true);

                //FormsAuthentication.SetAuthCookie(user.Id.ToString(), true);

                var identity = new ClaimsIdentity(new[] {
                    new Claim(ClaimTypes.Name, string.IsNullOrEmpty(user.fullname)?user.entryname:user.fullname),
                    new Claim(ClaimTypes.Email, string.IsNullOrEmpty(user.email)?"":user.email),
                    new Claim(ClaimTypes.PrimarySid, user.entryid.ToString())
                },
                                                  "ApplicationCookie");
                var ctx         = Request.GetOwinContext();
                var authManager = ctx.Authentication;
                authManager.SignIn(identity);

                if (Url.IsLocalUrl(returnUrl) &&
                    returnUrl.Length > 1 &&
                    returnUrl.StartsWith("/") &&
                    !returnUrl.StartsWith("//") &&
                    !returnUrl.StartsWith("/\\"))
                {
                    return(Redirect(returnUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                ViewBag.message = "Tên đăng nhập hoặc mật khẩu không đúng.";
            }
            return(View());
        }
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("Login"));
            }

            var user = AuthUser.GetByCode(loginInfo.Login.ProviderKey, loginInfo.Login.LoginProvider);

            if (user == null)
            {
                user               = new AuthUser();
                user.createdat     = DateTime.Now;
                user.updatedat     = DateTime.Now;
                user.entrycode     = loginInfo.Login.ProviderKey;
                user.entryname     = loginInfo.Login.ProviderKey;
                user.fullname      = loginInfo.DefaultUserName;
                user.email         = loginInfo.Email;
                user.logintype     = (int)LoginType.ExternalLogin;
                user.loginprovider = loginInfo.Login.LoginProvider;
                user.isactive      = true;
                user.entryid       = user.AddOrUpdate(user.entryid);
            }
            else
            {
                user.updatedat = DateTime.Now;
                user.lastlogin = DateTime.Now;
                user.AddOrUpdate(user.entryid);
            }

            // Sign in the user with this external login provider if the user already has a login

            //FormsAuthentication.SetAuthCookie(user.Id.ToString(), true);

            var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Name, user.fullname),
                new Claim(ClaimTypes.Email, string.IsNullOrEmpty(user.email)?"":user.email),
                new Claim(ClaimTypes.PrimarySid, user.entryid.ToString())
            },
                                              "ApplicationCookie");

            var ctx         = Request.GetOwinContext();
            var authManager = ctx.Authentication;

            authManager.SignIn(identity);

            return(RedirectToLocal(returnUrl));
            //var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
            //switch (result)
            //{
            //    case SignInStatus.Success:
            //        return RedirectToLocal(returnUrl);
            //    case SignInStatus.LockedOut:
            //        return View("Lockout");
            //    case SignInStatus.RequiresVerification:
            //        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
            //    case SignInStatus.Failure:
            //    default:
            //        // If the user does not have an account, then prompt the user to create an account
            //        ViewBag.ReturnUrl = returnUrl;
            //        ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
            //        return RedirectToLocal(returnUrl);
            //        //return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
            //}
        }