示例#1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            var error = 0;

            var checkPhone = _userService.CheckPhoneNumber(model.PhoneNumber);

            if (checkPhone == false)
            {
                ModelState.AddModelError(model.PhoneNumber, $"Phone number '{model.PhoneNumber}' is already taken.");
                error++;
            }

            if (!ModelState.IsValid)
            {
                error++;
            }

            //MM/dd/yyy
            var user = new AppUser
            {
                UserName     = model.UserName,
                Email        = model.Email,
                FullName     = model.FullName,
                PhoneNumber  = model.PhoneNumber,
                BirthDay     = model.BirthDay,
                Status       = Status.Active,
                Avatar       = string.Empty,
                Gender       = model.Gender,
                Address      = model.Address,
                DateCreated  = DateTime.Now,
                DateModified = DateTime.Now
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded || error > 0)
            {
                AddErrors(result);

                // If we got this far, something failed, redisplay form
                return(View(model));
            }

            _logger.LogInformation("User created a new account with password.");
            await _userManager.AddToRoleAsync(user, "Customer");

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);

            await _emailSender.SendEmailAsync(model.Email, "Confirm your email",
                                              $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

            //await _signInManager.SignInAsync(user, isPersistent: false);
            _logger.LogInformation("User created a new account with password.");

            return(RedirectToLocal("/login.html"));
        }