public async Task <IActionResult> CreateNewUser([FromBody] RegisterViewModel model) { if (ModelState.IsValid) { Regex regex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); Match match = regex.Match(model.Email.Trim().ToLower()); if (!match.Success) { return(new JsonResult(new { code = 0, Err = "" })); } var user = new MyUsers { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber, CodeConfirm = AccountHelpers.GenerateCodeConfirm(), IsActivated = true, LockoutEnabled = false, CreatedDate = DateTime.Now, DisplayName = model.FullName }; var roleInDb = _db.AspNetRoles.Where(x => x.Id == Int32.Parse(model.RoleId)).FirstOrDefault(); var result = await _userManager.CreateAsync(user, model.Password).ConfigureAwait(true); if (result.Succeeded) { var useraddress = new UserAddress { UserId = user.Id, WardId = model.WardId, ProvinceId = model.ProvinceId, DistrictId = model.DistrictId, PlaceDetails = model.PlaceDetail + _db.Ward.Find(model.WardId).WardName + "," + _db.Districts.Find(model.DistrictId).DistrictName + "," + _db.Provinces.Find(model.ProvinceId).ProvinceName }; _db.UserAddress.Add(useraddress); await _db.SaveChangesAsync(); #region Assign to Role, default Customer var resultRole = new IdentityResult(); if (roleInDb == null) { if (!await _roleManager.RoleExistsAsync(SD.Customer)) { await _roleManager.CreateAsync(new IdentityRole <int> { Name = SD.Customer }); } resultRole = await _userManager.AddToRoleAsync(user, SD.Customer); if (resultRole.Succeeded) { return(RedirectToAction(nameof(AccountManager))); } else { return(Json(new { code = 0, Err = "Không thể gán role cho user:"******"" })); } } else { resultRole = await _userManager.AddToRoleAsync(user, roleInDb.Name); if (resultRole.Succeeded) { Response.StatusCode = (int)HttpStatusCode.OK; return(Json(new { code = 1 })); } else { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { code = 0, Err = "Không thể gán role cho user:"******"" })); } } #endregion } } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(new JsonResult(new { code = 0, Err = "*Có lỗi xảy ra, vui lòng thử lại" })); }
//[Route("/[controller]/SignUpPost")] public async Task <IActionResult> SignUp(RegisterViewModel model) { ViewBag.Error = ""; if (ModelState.IsValid) { Regex regex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); Match match = regex.Match(model.Email.Trim().ToLower()); if (!match.Success) { return(new JsonResult(new { code = 0, Err = "" })); } var checkUser = _db.AspNetUsers.Where(x => x.Email == model.Email).FirstOrDefault(); if (checkUser != null) { return(Json(new { code = 0 })); } var user = new MyUsers { UserName = model.Email, Email = model.Email, DisplayName = model.FullName, LockoutEnabled = false, IsActivated = false, CreatedDate = DateTime.Now, PhoneNumber = model.PhoneNumber, CodeConfirm = AccountHelpers.GenerateCodeConfirm() }; var result = await _userManager.CreateAsync(user, model.Password).ConfigureAwait(true); //var result = CustomRepository.CreateUser(model.Email, model.PhoneNumber, model.Password, model.FullName, model.ProvinceId, model.DistricId, model.WardId, model.PlaceDetail); if (result.Succeeded) { //var userFromDb = _db.AspNetUsers.Where(x => x.Email == model.Email).FirstOrDefault(); if (!await _roleManager.RoleExistsAsync(SD.Customer)) { await _roleManager.CreateAsync(new IdentityRole <int> { Name = SD.Customer }); } await _userManager.AddToRoleAsync(user, SD.Customer); var userAddress = new UserAddress { UserId = user.Id, WardId = model.WardId, ProvinceId = model.ProvinceId, DistrictId = model.DistrictId, PlaceDetails = model.PlaceDetail + _db.Ward.Find(model.WardId).WardName + "," + _db.Districts.Find(model.DistrictId).DistrictName + "," + _db.Provinces.Find(model.ProvinceId).ProvinceName }; _db.UserAddress.Add(userAddress); await _db.SaveChangesAsync(); return(RedirectToAction("SignIn", "Account")); } else { ViewBag.Error = "Người dùng đã tồn tại"; return(RedirectToAction(nameof(SignUp))); } } return(new JsonResult(new { code = 0, Err = "*Có lỗi xảy ra, vui lòng thử lại" })); }