Beispiel #1
0
        //[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 result = CustomRepository.CreateUser(model.Email, model.PhoneNumber, model.Password, model.FullName, model.ProvinceId, model.DistricId, model.WardId, model.PlaceDetail);
                if (result != null)
                {
                    EmailHelper.SendConfirmEmail(result);
                    var user = _db.TblUsers.Where(x => x.Email.Equals(model.Email)).FirstOrDefault();
                    var re   = await CustomRepository.AddUserToRoleAsync(user, 3);

                    if (re == true)
                    {
                        return(RedirectToAction("SignIn", "Account", new { Email = user.Email }));
                    }
                }
                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" }));
        }
        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 TblUsers
                //{
                //    UserName = model.UserName,
                //    Email = model.Email,
                //    PhoneNumber = model.PhoneNumber,
                //    Password = model.Password
                //    //DisplayName = model.FullName
                //};
                var result = CustomRepository.CreateUser(model.Email, model.PhoneNumber, model.Password, model.FullName, model.ProvinceId, model.DistricId, model.WardId, model.PlaceDetail);
                if (result != null)
                {
                    var user = _db.TblUsers.Where(x => x.Email.Equals(model.Email)).FirstOrDefault();
                    var re   = await CustomRepository.AddUserToRoleAsync(user, 3);

                    if (re == true)
                    {
                        return(new JsonResult(new { code = 1, Err = "Thêm thành công" }));
                    }
                }
                else
                {
                    ViewBag.Error = "Người dùng đã tồn tại";
                    return(new JsonResult(new { code = 0, Err = "*Người dùng đã tồn tại" }));
                }
            }
            return(new JsonResult(new { code = 0, Err = "*Có lỗi xảy ra, vui lòng thử lại" }));
        }