Exemplo n.º 1
0
        public HttpResponseMessage PostRegister(AccountModel model)
        {
            try
            {
                if (String.IsNullOrEmpty(model.Email) || String.IsNullOrEmpty(model.Password) || !EmailVerification.IsValidEmail(model.Email))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable));
                }

                if (DataContext.User.Any(x => x.Email == model.Email))
                {
                    return(Request.CreateResponse(HttpStatusCode.Conflict));
                }

                var newUser = new User
                {
                    Username = String.IsNullOrEmpty(model.Username) ? model.Email.Split('@')[0] : model.Username,
                    Email    = model.Email,
                    Password = PasswordHash.CreateHash(model.Password)
                };
                DataContext.User.Add(newUser);
                DataContext.Commit();
                model.UserId    = newUser.Id;
                model.Username  = newUser.Username;
                model.OpenGames = new OpenGamesModel();
                return(Request.CreateResponse(HttpStatusCode.Created, model));
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage PostRegister(AccountModel model)
        {
            try
            {
                if (String.IsNullOrEmpty(model.Email) || String.IsNullOrEmpty(model.Password) || !EmailVerification.IsValidEmail(model.Email))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable));
                }

                if (UserManager.FindByEmail(model.Email) != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.Conflict, "Email already exists"));
                }

                var newUser = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                IdentityResult user = UserManager.Create(newUser, model.Password);

                if (user.Succeeded == false)
                {
                    String passwordError = user.Errors.FirstOrDefault(err => err.Contains("Passwords"));
                    if (!String.IsNullOrEmpty(passwordError))
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotAcceptable, passwordError));
                    }
                    return(Request.CreateResponse(HttpStatusCode.NotAcceptable, user.Errors));
                }

                ApplicationUser createdUser = UserManager.FindByEmail(model.Email);

                model.Password = null;
                model.Username = null;
                model.Email    = null;
                model.UserId   = createdUser.Id;


                return(Request.CreateResponse(HttpStatusCode.Created, model));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }