Esempio n. 1
0
        private User CreateUser(UserModel userModel)
        {
            var user = new User();
            user.email = userModel.Email;
            user.password = EncryptPassword(userModel.Password);
            user.lastName = userModel.LastName;
            user.firstName = userModel.FirstName;
            user.createdDate = DateTime.Now;
            user.description = userModel.Description;
            user.sex = userModel.Sex;

            db.Users.Add(user);
            db.SaveChanges();

            return user;
        }
Esempio n. 2
0
        public HttpResponseMessage Register(UserModel userModel)
        {
            String email = userModel.Email;
            String password = userModel.Password;

            HttpResponseMessage response;
            if (ModelState.IsValid)
            {
                var existingUser = db.Users.FirstOrDefault(u => u.email == userModel.Email);
                if (existingUser != null)
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "User already exist.");
                }

                if(!userModel.Password.Equals(userModel.RePassword)) {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Passwords are not the same.");
                }

                //Create user and save to database
                var user = CreateUser(userModel);

                object dbUser;

                //Create token
                var token = CreateToken(user, out dbUser);

                response = Request.CreateResponse(new { dbUser, token });
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, new { success = false });
            }

            return response;
        }