//[ValidateAntiForgeryToken]
        public async Task <IActionResult> Register([FromBody] RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var user = new HamsterSqueaksUser {
                    UserName = viewModel.Email, Email = viewModel.Email
                };
                var result = await _userManager.CreateAsync(user, viewModel.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    await _emailSender.SendEmailConfirmationAsync(viewModel.Email, "");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation("User created a new account with password.");

                    // Do not return the passwords.
                    viewModel.Password        = "";
                    viewModel.ConfirmPassword = "";

                    return(Ok(viewModel));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(ModelState));
        }
Exemple #2
0
 /// <summary>
 /// Default contructor.
 /// </summary>
 /// <param name="model">The HamsterSqueaksUser model to base the UserViewModel of.</param>
 public UserViewModel(HamsterSqueaksUser model = default(HamsterSqueaksUser))
 {
     _model = model ?? new HamsterSqueaksUser();
 }