Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return(Response(model, "Por favor verifique os campos preenchidos."));
            }

            var user = new ApplicationUser {
                Name = model.Name, UserName = model.Email, Email = model.Email
            };

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

            if (result.Succeeded)
            {
                // User claim for write customers data
                //await _applicationUserManager.AddClaimAsync(user, new Claim("Users", "Admin"));

                if (!string.IsNullOrEmpty(model.Role))
                {
                    if (Roles.Contains(model.Role))
                    {
                        await _applicationUserManager.AddToRoleAsync(user, model.Role);
                    }
                }

                //await _signInManager.SignInAsync(user, false);
                model.Clean();
                return(Response(model, "O usuário foi cadastro com sucesso!"));
            }

            AddIdentityErrors(result);

            model.Clean();

            return(Response(model, "Falha ao cadastrar o usuário."));
        }