public tbl_Account authorize()
        {
            tbl_Account acc = new tbl_Account();
            if (!string.IsNullOrEmpty(Request.QueryString["code"]))
            {
                string code = Request.QueryString["code"];
                string json = GoogleConnect.Fetch("me", code);
                GoogleProfile profile = new JavaScriptSerializer().Deserialize<GoogleProfile>(json);
                string emailgg = profile.Emails.Where(email => email.Type == "account").FirstOrDefault().Value;
                FormsAuthentication.SetAuthCookie(emailgg, false);
                Session["Member"] = emailgg;
                Session["Name"] = profile.DisplayName;

                var check = db.tbl_Account.Where(e => e.Email == emailgg).FirstOrDefault();
                if (check == null)
                {
                    acc = new tbl_Account()
                     {
                         Username=emailgg,
                         HoVaTen = profile.DisplayName,
                         Email = emailgg,
                         GioiTinh = profile.Gender == "male" ? false : true,
                         Role = 4,
                         Password = "******"

                     };
                    db.tbl_Account.Add(acc);
                    db.SaveChanges();
                }
                else
                    acc = check;
            }
            return acc;
        }
 public ActionResult DoiMK(tbl_Account member)
 {
     //    db.Entry(member).State = EntityState.Modified;
     //    db.SaveChanges();
     //    ViewBag.ac = db.tbl_Account;
     return RedirectToAction("Index", "Member", new { @username = Session["Member"] });
 }
        public ActionResult Index(string username)
        {
            List<String> error = new List<string>();
            if (username == null)
                error.Add("Username is required.");
            tbl_Account acc = new tbl_Account();
            acc = db.tbl_Account.FirstOrDefault(m => m.Username.Equals(username));
            if (acc == null)
                error.Add("Username is not exists.");
            ViewBag.error = error;
            if (error.Count > 0)
                return View();
            string body = "";
            try
            {
                string passwordReset = RandomString(10);
                MD5 md5Hash = MD5.Create();
                string hash = GetMd5Hash(md5Hash, passwordReset);
                acc.Password = hash;
                db.SaveChanges();
                body = "Password mới của bạn là: " + passwordReset;
            }
            catch (Exception e) { error.Add(e.ToString()); ViewBag.error = error; return View(); }

            string smtpUserName = "******";
            string smtpPassword = "******";
            string smtpHost = "smtp.gmail.com";
            int smtpPort = 25;
            string emailTo = acc.Email;
            string subject = "RESET PASSWORD";

            bool kq = Send(smtpUserName, smtpPassword, smtpHost, smtpPort,
                    emailTo, subject, body);
            if (kq) error.Add("We have send an email with new password. Please check email!");
            else error.Add("Send email fail. Make sure your email exists");

            return View();
        }
        public ActionResult FacebookCallback(string code)
        {
            try
            {
                var fb = new FacebookClient();
                dynamic result = fb.Post("oauth/access_token", new

                {

                    client_id = "769429839779017",

                    client_secret = "910688593eef875bfca0e70767032c0d",

                    redirect_uri = RedirectUri.AbsoluteUri,

                    code = code

                });
                var accessToken = result.access_token;

                //Luu access token ma fb tra ve vao session
                Session["AccessToken"] = accessToken;

                fb.AccessToken = accessToken;

                dynamic info = fb.Get("me?fields=first_name,last_name,id,email");
                string email = info.email;
                string name = info.first_name;
                string username = info.id;
                FormsAuthentication.SetAuthCookie(email, false);
                Session["Member"] = email;
                Session["Name"] = name;
                var lstAccount = db.tbl_Account.Where(e => e.Username.Equals(email)).ToList();
                if (lstAccount.Count() == 0)
                {
                    var acc = new tbl_Account { Username = email, Email = email, HoVaTen = name, Role = 4 };
                    db.tbl_Account.Add(acc);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Member", new { @username= email});
                }
                else return RedirectToAction("Index", "Member", new { @username =email });
            }
            catch
            {
                return RedirectToAction("Index", "TrangChu");
            }
        }
        public ActionResult EditMod(tbl_Account mod, int id)
        {
            if (Session["Admin"] == null && Session["SA"] == null)
                RedirectToAction("Index", "Login");
            List<String> error = new List<String>();
            ViewBag.error = "";

            try
            {
                mod.ID_DanhMuc = id;
                db.Entry(mod).State = EntityState.Modified;
                db.SaveChanges();
                error.Add("Edit success!");
                ViewBag.error = error;
            }
            catch
            {
                error.Add("Error!");
                ViewBag.error = error;
            }
            ViewBag.model = db.tbl_DanhMuc.Where(m => m.ID_DanhMucCha == null).ToList();
            return View();
        }
        public ActionResult AddAdmin(tbl_Account model, string confirmPassword)
        {
            if (Session["Admin"] == null && Session["SA"] == null)
                RedirectToAction("Index", "Login");
            List<String> error = new List<string>();

            var account = from i in db.tbl_Account
                          where i.Username.Equals(model.Username)
                          select i;
            if (account != null)
            {
                foreach (var i in account)
                {

                    bool OK = stringCompare(i.Username, model.Username);
                    if (OK)
                        error.Add("Username is already exists.");
                }
            }

            try
            {
                DateTime dt = (DateTime)model.NgaySinh;
            }
            catch (Exception)
            {
                error.Add("NgaySinh is invalid!");
            }
            try
            {
                long phone = long.Parse(model.DienThoai);
            }
            catch (Exception)
            {
                error.Add("DienThoai much be a number.");
            }

            if (model.Password != null && (!model.Email.Contains("@") || model.Email.Length < 10 || !model.Email.Contains(".")))
                error.Add("Email is not exists.");
            if (model.Password != null && model.Password.Length < 6)
                error.Add("Password must be at least 6 characters long");
            if (confirmPassword != null && confirmPassword.Length < 6)
                error.Add("Confirm Password must be at least 6 characters long");
            if (model.Password != null && confirmPassword != null)
                if (!model.Password.Equals(confirmPassword))
                    error.Add("Confirm Password and Password is not match");
            if (confirmPassword == null)
                error.Add("Confirm is required.");
            if (model.HoVaTen == null)
                error.Add("HoVaTen field is required.");
            if (model.Username == null)
                error.Add("Username field is required.");
            if (model.Password == null)
                error.Add("Password field is required.");
            if (model.DienThoai == null)
                error.Add("DienThoai field is required.");
            if (model.DiaChi == null)
                error.Add("DiaChi field is required.");
            if (model.GioiTinh == null)
                error.Add("GioiTinh field is required.");
            if (model.Email == null)
                error.Add("Email field is required.");

            if (error.Count > 0)
            {
                ViewBag.error = error;
                return View();
            }
            try
            {
                MD5 md5Hash = MD5.Create();
                string password = model.Password;
                string hash = GetMd5Hash(md5Hash, password);
                model.Password = hash;
                model.Role = 2;
                db.tbl_Account.Add(model);
                db.SaveChanges();
                error.Add("Create new Admin success!");
                ViewBag.error = error;
            }
            catch (Exception)
            {
                error.Add("Username is already exists.");
                ViewBag.error = error;
            }
            return View();
        }
        public ActionResult WebClick(int? id)
        {
            tbl_Website web = new tbl_Website();
            tbl_Account acc = new tbl_Account();
            long coinPerClick = 0;
            long coinPerDay = 0;
            int id_acc;
            DateTime lastClick;
            long timeSpan = 0;
            try
            {
                DateTime now = DateTime.Now;

                web = db.tbl_Website.Where(m => m.ID_Web == id).FirstOrDefault();
                if (web.LastClick != null)
                {
                    lastClick = (DateTime)web.LastClick;
                    timeSpan = (now - lastClick).Minutes;
                }
                else
                {
                    web.LastClick = now;
                    timeSpan = 11;
                }
                if (timeSpan > 10)
                {
                    coinPerClick = (long)web.CoinPerClick;
                    id_acc = (int)web.ID_Account;
                    acc = db.tbl_Account.Where(m => m.ID_Account == id_acc).FirstOrDefault();
                    coinPerDay = (long)acc.CoinPerDay;
                    if (coinPerDay >= coinPerClick)
                        acc.CoinPerDay -= coinPerClick;
                    else if (acc.TotalCoin >= coinPerClick)
                        acc.TotalCoin -= coinPerClick;
                    if (coinPerDay < coinPerClick && acc.TotalCoin < coinPerClick)
                        web.Active = 0;
                    web.LastClick = now;
                    web.TongSoClick++;
                }
                else
                {
                    web.SoClickFree++;
                    web.TongSoClick++;
                }

                db.SaveChanges();
            }catch(Exception)
            {

            }
            HomeViewModel h = new HomeViewModel();
            return View(h);
        }