public ActionResult Add(IdentityRole model)
        {
            try
            {
                db.Roles.Add(model);
                db.SaveChanges();
                var Projectdb = new ProjectMvcDbContext();
                foreach (var p in Projectdb.WebActions)
                {
                    var perm = new Permission
                    {
                        Allow = false,
                        RoleId = model.Id,
                        WebActionId = p.Id
                    };
                    Projectdb.Permissions.Add(perm);
                }

                Projectdb.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception Ex)
            {
                ModelState.AddModelError("", Ex.Message);
                ViewBag.Role = db.Roles;
                return View("Index", model);
            }
        }
 public ActionResult EditProfile()
 {
     using (var db = new ProjectMvcDbContext())
     {
         var model = db.Customers.Find(User.Identity.Name);
         return PartialView("_EditProfile", model);
     }
 }
 public void Add(int Id)
 {
     try
     {
         var prod = Items.Single(p => p.Id == Id);
         prod.Quantity++;
     }
     catch
     {
         using (var db = new ProjectMvcDbContext())
         {
             var prod = db.Products.Find(Id);
             prod.Quantity = 1;
             Items.Add(prod);
         }
     }
 }
 public ActionResult EditProfile(Customer model, HttpPostedFileBase upAnh)
 {
     
     if (upAnh != null && upAnh.ContentLength > 0)
     {
         if (System.IO.File.Exists(Server.MapPath("~/Images/Customers/" + model.Photo)))
         {
             System.IO.File.Delete(Server.MapPath("~/Images/Customers/" + model.Photo));
         }
         var photoName = model.Id + upAnh.FileName.Substring(upAnh.FileName.LastIndexOf("."));
         string path = Path.Combine(Server.MapPath("~/Images/Customers") + Path.GetFileName(photoName));
         upAnh.SaveAs(path);
         model.Photo = photoName;
     }
     if (ModelState.IsValid)
     {
         using (var db = new ProjectMvcDbContext())
         {
             try
             {
                 db.Entry(model).State = EntityState.Modified;
                 db.SaveChanges();
                 ViewBag.Message = "Success";
                 ModelState.AddModelError("", "Cập nhật Profile thành công");
                 return View("Error");
             }
             catch (Exception Ex)
             {
                 ViewBag.Message = "Error";
                 ModelState.AddModelError("", Ex.Message);
                 return View("Error");
             }
         }
     }
     ViewBag.Message = "Error";
     return View("Error");
 }
        public ActionResult ForgotPassword(string UserName, string Email)
        {
            var user = UserManager.FindByName(UserName);
            if (user != null)
            {
                using (var db = new ProjectMvcDbContext())
                {
                    var custom = db.Customers.Find(UserName);
                    if (custom.Email == Email)
                    {
                        var newcode = Guid.NewGuid().ToString();
                        UserManager.RemovePassword(user.Id);
                        UserManager.AddPassword(user.Id, newcode);

                        var to = Email;
                        var subject = "Reset Password";
                        var body = "Sử dụng chuỗi ký tự này để thay đổi mật khẩu " + newcode;

                        XMail.Send(to, subject, body);
                    }
                    else
                    {
                        ViewBag.Message = "Error";
                        ModelState.AddModelError("", "Email không hợp lệ");
                        return View("Error");
                    }
                }
            }
            else
            {
                ViewBag.Message = "Error";
                ModelState.AddModelError("", "Sai tên đăng nhập");
                return View("Error");
            }
            ViewBag.Message = "Success";
            ModelState.AddModelError("", "Hãy kiểm tra Email để biết mật khẩu mới. Đăng nhập để thay đổi mật khẩu");
            return View("Error");
        }
        public async Task<ActionResult> Register(Customer model, string ConfirmPassword, HttpPostedFileBase upAnh)
        {
            //var photo = Request.Files["upAnh"];
            //if (photo.ContentLength > 0)
            //{
            //    var photoName = model.Id + photo.FileName.Substring(photo.FileName.LastIndexOf("."));
            //    photo.SaveAs(Server.MapPath("~/Images/Customers/" + photoName));
            //    model.Photo = photoName;
            //}
            if (upAnh != null && upAnh.ContentLength > 0)
            {
                var photoName = model.Id + upAnh.FileName.Substring(upAnh.FileName.LastIndexOf("."));
                string path = Path.Combine(Server.MapPath("~/Images/Customers"), Path.GetFileName(photoName));
                upAnh.SaveAs(path);
                model.Photo = photoName;
            }
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.Id };
                if(model.Password != null)
                {
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        using (var db = new ProjectMvcDbContext())
                        {
                            db.Customers.Add(model);
                            db.SaveChanges();
                        }

                        //var to = model.Email;
                        //var subject = "Welcome to ........";
                        //var url = Request.Url.AbsoluteUri.Replace("Register", "Activate/" + XString.ToBase64(model.Id.ToString()));
                        //var body = "Vui lòng nhấp vào liên kết sau để kích hoạt tài khoản <a href='" + url + "'>Activate</a>";
                        //XMail.Send(to, subject, body);

                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ViewBag.Message = "Error";
                        ModelState.AddModelError("", "Register Fail");
                        return View("Error");
                    }
                }
                else
                {
                    ViewBag.Message = "Error";
                    ModelState.AddModelError("", "Password cannot null");
                    return View("Error");
                }
            }
            // If we got this far, something failed, redisplay form
            ViewBag.Message = "Error";
            return View("Error");
        }
        public async Task<ActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindAsync(model.UserName, model.Password);
                if (user != null)
                {
                    using (var db = new ProjectMvcDbContext())
                    {
                        var custom = db.Customers.Find(model.UserName);
                        if (custom.Activated)
                        {
                            await SignInAsync(user, model.RememberMe);
                            // Stay on same page
                            return Redirect(Request.UrlReferrer.ToString());
                        }
                        else
                        {
                            ViewBag.Message = "Error";
                            ModelState.AddModelError("", "Tài khoản chưa kích hoạt !");
                            return View("Error");
                        }
                    }
                }
                else
                {
                    ViewBag.Message = "Error";
                    ModelState.AddModelError("", "Sai username or password.");
                    return View("Error");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.Message = "Error";
            return View("Error");
        }
        public ActionResult Activate(string Id)
        {
            using (var db = new ProjectMvcDbContext())
            {
                var active = db.Customers.Find(Id.FromBase64());
                if (active != null)
                {
                    active.Activated = true;
                    db.SaveChanges();

                    ViewBag.Message = "Success";
                    ModelState.AddModelError("", "Congratulation ! your account is activated !");
                    return View("Error");
                }
            }
            return RedirectToAction("Register");
        }