public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (!ModelState.IsValid) { return(Response()); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { result = await _userManager.AddClaimsAsync(user, new List <Claim> { new Claim(type: "Contatos", value: "Ver"), new Claim(type: "Contatos", value: "Gerenciar"), new Claim(type: "Contatos", value: "GerenciarTelefones") }); if (result.Succeeded) { var usuarioDados = new UsuarioDadosViewModel { Id = Guid.Parse(user.Id), Nome = model.Nome, CPF = model.CPF }; var retorno = _usuarioDadosAppService.Add(usuarioDados); if (!retorno.ValidationResult.IsValid) { await _userManager.DeleteAsync(user); return(Response(viewModel: retorno, result: null)); } _logger.LogInformation(1, "Usuario criado com sucesso!"); var response = GerarTokenUsuario(new LoginViewModel { Email = model.Email, Password = model.Password }); return(Response(viewModel: null, result: response)); } await _userManager.DeleteAsync(user); AdicionarErrosIdentity(result); return(Response()); } AdicionarErrosIdentity(result); return(Response()); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; user.Claims.Add(new IdentityUserClaim <string> { ClaimType = "Contatos", ClaimValue = "Ver" }); user.Claims.Add(new IdentityUserClaim <string> { ClaimType = "Contatos", ClaimValue = "Gerenciar" }); user.Claims.Add(new IdentityUserClaim <string> { ClaimType = "Contatos", ClaimValue = "GerenciarTelefones" }); var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var usuarioDados = new UsuarioDadosViewModel { Id = Guid.Parse(user.Id), Nome = model.Nome, CPF = model.CPF }; var retorno = _usuarioDadosAppService.Add(usuarioDados); if (!retorno.ValidationResult.IsValid) { retorno .ValidationResult .Errors.ToList() .ForEach(e => ModelState.AddModelError(string.Empty, e.ErrorMessage)); await _userManager.DeleteAsync(user); return(View(model)); } // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", // $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>"); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation(3, "User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }