public HttpResponseMessage RegisterUser(UserRegisterModel userToRegister) { UserValidator.ValidateAuthCode(userToRegister.AuthCode); UserValidator.ValidateNickname(userToRegister.Nickname); UserValidator.ValidateUsername(userToRegister.Username); User newUser = null; try { newUser = UsersMapper.ToUserEntity(userToRegister); } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid user register model provided!")); } usersRepository.Add(newUser); User inDbUser = this.usersRepository.GetByUsernameAndAuthCode(newUser.Username, newUser.AuthCode); inDbUser.SessionKey = UserValidator.GenerateSessionKey(inDbUser.ID); this.usersRepository.Update(inDbUser.ID, inDbUser); UserLoggedModel loggedUser = new UserLoggedModel() { Nickname = inDbUser.Nickname, SessionKey = inDbUser.SessionKey }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, loggedUser); response.Headers.Location = new Uri(Url.Link("NewsApi", new { sessionKey = loggedUser.SessionKey })); return(response); }