Example #1
0
        public async Task <ActionResult> CreateAdmin([FromServices] UserManager <ApplicationUser> userManager,
                                                     [FromServices] AccessManager accessManager,
                                                     [FromBody] RegisterAdmin registerAdmin)
        {
            var user = new ApplicationUser
            {
                UserName    = registerAdmin.UserName,
                Email       = registerAdmin.Email,
                PhoneNumber = registerAdmin.PhoneNumber,
            };

            var result = userManager.CreateAsync(user, registerAdmin.Password).Result;

            if (result.Succeeded)
            {
                var usuarioSAlvo  = userManager.FindByNameAsync(user.UserName).Result;
                var usuarioAcesso = new User();
                usuarioAcesso.UserID   = usuarioSAlvo.Id;
                usuarioAcesso.Password = usuarioSAlvo.PasswordHash;
                usuarioAcesso.Email    = usuarioSAlvo.Email;
                var resultado = accessManager.GenerateTokenAdmin(usuarioAcesso);
                return(Created($"registerUser/{resultado}", new { resultado }));
            }
            else
            {
                return(BadRequest("Usuário ou senha inválidos"));
            }
        }