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(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 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 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"); }