Esempio n. 1
0
        public async Task <IActionResult> RegisterUser(RegisterUserDto userDto)
        {
            User           user   = RegisterUserMapper.Map(userDto);
            IdentityResult result = await signInManager.UserManager.CreateAsync(user, user.PasswordHash);

            if (result.Succeeded)
            {
                return(Ok());
            }
            else
            {
                return(Unauthorized());
            }
        }
Esempio n. 2
0
        public HttpResponseMessage Register(UserRegister userToRegister)
        {
            try
            {
                if (userToRegister != null && ModelState.IsValid)
                {
                    UserDTO             userDTO  = RegisterUserMapper.ToDTO(userToRegister);
                    var                 user     = userLogic.Register(userDTO);
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
                    return(response);
                }
                else
                {
                    var validationErrors = new List <string>();
                    foreach (var state in ModelState)
                    {
                        foreach (var error in state.Value.Errors)
                        {
                            validationErrors.Add((error.ErrorMessage));
                        }
                    }

                    var jsonerrors = JsonConvert.SerializeObject(new
                    {
                        error = validationErrors
                    });
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, JsonConvert.DeserializeObject(jsonerrors)));
                }
            }
            catch (UserAlreadyExists e)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, new { error = e.Message });
                return(response);
            }
            catch (Exception e)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, e.Message);
                return(response);
            }
        }