Esempio n. 1
0
        public async Task <IActionResult> Create(RegisterViewModel registerViewModel)
        {
            IActionResult actionResult = null;

            if (this.ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = registerViewModel.UserName,
                    Email    = registerViewModel.Email
                };

                var existentUser = await this.userManager
                                   .FindByNameAsync(registerViewModel.UserName);

                if (existentUser == null)
                {
                    var identityResult = await this.userManager.CreateAsync(
                        user,
                        registerViewModel.Password);

                    if (identityResult.Succeeded)
                    {
                        actionResult = this.RedirectToAction(nameof(this.Index));
                    }
                    else
                    {
                        const string message =
                            "Password must be at least 6 characters, " +
                            "requires at least one non-alphanumeric character," +
                            "at least one digit, " +
                            "at least one lowercase character," +
                            "and at least one uppercase character.";

                        this.ModelState.AddModelError(
                            nameof(registerViewModel.Password),
                            message);

                        registerViewModel.ClearPassword();

                        actionResult = this.View(registerViewModel);
                    }
                }
                else
                {
                    this.ModelState.AddModelError(
                        nameof(registerViewModel.UserName),
                        "The user name already exists");

                    registerViewModel.ClearPassword();

                    actionResult = this.View(registerViewModel);
                }
            }
            else
            {
                registerViewModel.ClearPassword();

                actionResult = this.View(registerViewModel);
            }

            return(actionResult);
        }