Example #1
0
        public JsonResult Register(RegisterDataModel newAccountModel)
        {
            if (ModelState.IsValid)
            {
                // 密碼加密
                newAccountModel.LoginPassword = Hash(newAccountModel.LoginPassword);

                // 避免角色被亂改
                if (newAccountModel.Role != "Coach" && newAccountModel.Role != "Student")
                {
                    return(Json("Wrong Role"));
                }

                try
                {
                    using (AccountDAC dac = new AccountDAC())
                    {
                        dac.CreateNewAccount(newAccountModel);
                    }
                    return(Json("SUCCESS"));
                }
                catch
                {
                    return(Json("帳號建立失敗!請重試"));
                }
            }
            else
            {
                return(Json("失敗!請重試"));
            }
        }
Example #2
0
 public static UserEntity MapToEntity(RegisterDataModel model)
 {
     return(new UserEntity
     {
         FirstName = model.FirstName,
         LastName = model.LastName,
         Username = model.Username
     });
 }
Example #3
0
 public IActionResult CreateUser([FromBody] RegisterDataModel userRegistrationModel)
 {
     try
     {
         var userEntity = UserEntity.MapToEntity(userRegistrationModel);
         var user       = _userService.Create(userEntity, userRegistrationModel.Password);
         return(Ok());
     }
     catch (AuthenticationException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex.Message));
     }
 }
        public async Task <IActionResult> Register([FromBody] RegisterDataModel model)
        {
            var hasUser = await _mediatr.Send(new HasUser.Query(model.Email));

            if (!hasUser)
            {
                string errorMessage = string.Empty;
                if (ModelState.IsValid)
                {
                    if (ValidatePassword(model.Password, ref errorMessage))
                    {
                        await _mediatr.Send(new CreateUser.Command(model.Email, model.Password));

                        string tokenString = CreateToken(model.Email, model.Email);

                        return(Ok(new
                        {
                            Username = model.Email,
                            Email = model.Email,
                            Token = tokenString
                        }));
                    }
                }
                else
                {
                    return(Ok(new
                    {
                        ErrorMessage = string.Join(',', ModelState.Values.SelectMany(m => m.Errors)
                                                   .Select(e => e.ErrorMessage)
                                                   .ToList())
                    }));
                }

                return(Ok(new { ErrorMessage = errorMessage }));
            }
            else
            {
                return(Ok(new { ErrorMessage = string.Format("User with email {0} has already registered", model.Email) }));
            }
        }
Example #5
0
        /// <summary>
        /// 新增帳號資料
        /// </summary>
        /// 2016/04/05 by Yohey
        /// <param name="newAccountModel"></param>
        public void CreateNewAccount(RegisterDataModel newAccountModel)
        {
            // 組合生日日期
            string birthday = newAccountModel.Birthday_Year + "/" + newAccountModel.Birthday_Month + "/" + newAccountModel.Birthday_Day;

            StringBuilder sql = new StringBuilder();

            sql.AppendLine(@"
                INSERT INTO AccountData
                    (Role, UserId, LoginPassword, Sex, Birthday,
                     EmailAddress, Address_County, Address_Township, Address_Detail, MobilePhone,
                     Memo, CreateDate)
                VALUES 
                    ({0}, {1}, {2}, {3}, CONVERT(DATETIME, {4}),
                     {5}, CONVERT(INT, {6}), CONVERT(INT, {7}), {8}, {9},
                     {10}, GETDATE())
            ");
            dbConn.ExecuteCommand(sql.ToString(),
                                  newAccountModel.Role, newAccountModel.UserId, newAccountModel.LoginPassword, newAccountModel.Sex, birthday,
                                  newAccountModel.EmailAddress, newAccountModel.Address_County, newAccountModel.Address_Township, newAccountModel.Address_Detail ?? "", newAccountModel.MobilePhone,
                                  newAccountModel.Memo ?? "");
        }
Example #6
0
        public IActionResult CreateUser([FromBody] RegisterDataModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Email == null || model.Email == "")
                {
                    return(BadRequest("Email is required"));
                }

                if (ValidationHelper.ValidateEmail(model.Email) == false)
                {
                    return(BadRequest("Valid email address is required"));
                }

                User existing = UserHelper.GetUserByEmail(model.Email);
                if (existing != null)
                {
                    return(BadRequest("Email address already used."));
                }

                if (model.Password == null || model.Password == "")
                {
                    return(BadRequest("Password is required"));
                }

                if (model.Password != model.ConfirmPassword)
                {
                    return(BadRequest("Passwords do not match"));
                }

                if (!UserHelper.IsValidPassword(model.Password))
                {
                    return(BadRequest("Password is not complex enough."));
                }



                //Create the user
                User user = new User();
                user.Email    = model.Email;
                user.Salt     = UserHelper.CreatUserSalt();
                user.Password = HasherHelper.GetHash(model.Password + user.Salt);

                //As part of this demo, manually activate the account here. There are activation services available - just finish tying in the email logic.
                user.EmailValidated = true;

                bool result = UserHelper.CreateUser(user);
                if (result)
                {
                    return(Ok(new IdResponse()
                    {
                        Id = user.Id
                    }));
                }
                else
                {
                    return(BadRequest("Could not create user profile."));
                }
            }
            else
            {
                return(BadRequest("Invalid data"));
            }
        }