public async Task <ActionResult> DangKyCongDan(DangKyCongDanViewModel viewModel) { if (ModelState.IsValid) { // Lấy thông tin chức vụ string roleName = ""; if (viewModel.ChucVuId == ChucVu.Admin) { roleName = "Admin"; } else if (viewModel.ChucVuId == ChucVu.BaoVeDanPho) { roleName = "BaoVeDanPho"; } else if (viewModel.ChucVuId == ChucVu.CanhSatKhuVuc) { roleName = "CanhSatKhuVuc"; } else if (viewModel.ChucVuId == ChucVu.CongDan) { roleName = "CongDan"; } // Khởi tạo biến để thao tác với ASP.NET Identity var UserManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); var SignInManager = HttpContext.GetOwinContext().Get <ApplicationSignInManager>(); var user = new ApplicationUser { UserName = viewModel.Email, Email = viewModel.Email }; var result = await UserManager.CreateAsync(user, viewModel.Password); if (result.Succeeded) { // Thêm quyền cho người dùng var roleresult = UserManager.AddToRole(user.Id, roleName); // Thêm cột IdentityId cho người dùng var nguoiDung = db.NguoiDungs.Find(viewModel.Id); nguoiDung.IdentityId = user.Id; db.SaveChanges(); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Admin")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(viewModel)); }
public ActionResult DangKyCongDan(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } NguoiDung nguoiDung = db.NguoiDungs.Include(m => m.ChucVu).FirstOrDefault(m => m.Id == id); if (nguoiDung == null) { return(HttpNotFound()); } var viewModel = new DangKyCongDanViewModel { Id = nguoiDung.Id, HoTen = nguoiDung.HoTen, ChucVuId = nguoiDung.ChucVuId, ChucVu = nguoiDung.ChucVu.Ten }; return(View(viewModel)); }