public ActionResult Register([Bind(Include = "username, email, firstName, lastName, phone, dob")] Account registerAccount) { if (ModelState.IsValid) { registerAccount.Gender = Request["gender"] == "male"; //ModelState.IsValid: whether auto binding request //parameter to object account field is correct registerAccount.Username = registerAccount.Username.ToLower(); registerAccount.Email = registerAccount.Email.ToLower(); //Step 1 check if username exists ? string duplicateUsername = ""; string duplicateEmail = ""; bool canAdd = true; if (accountService.IsDuplicatedUsername(registerAccount.Username)) { duplicateUsername = "******" + registerAccount.Username + "' is duplicated"; canAdd = false; } if (accountService.IsDuplicatedEmail(registerAccount.Email)) { duplicateEmail = "Email '" + registerAccount.Email + "' is duplicated"; canAdd = false; } if (!canAdd) { var viewModel = new AccountRegisterViewModel { Username = registerAccount.Username, Email = registerAccount.Email, FirstName = registerAccount.FirstName, LastName = registerAccount.LastName, Gender = registerAccount.Gender, Dob = registerAccount.DOB, Phone = registerAccount.Phone, DuplicateEmailErrorMessage = duplicateEmail, DuplicateUsernameErrorMessage = duplicateUsername }; ViewBag.message = @"<script>$('.login-form').css('display', 'none');$('.register-form').css('display', 'block');$('.show-login-form').removeClass('active');$('.show-register-form').addClass('active');</script>"; return(View("~/Views/Home/login.cshtml", viewModel)); } //halting password to store in database //NOTE: do not auto binding password at first registerAccount.Password = EncryptPasswordUtil.EncryptPassword(Request["password"], out string key); registerAccount.PasswordSalt = key; //set roleID, startDate, isActive registerAccount.StartDate = DateTime.Now; registerAccount.IsActive = true; registerAccount.RoleId = 2; // default is member if (accountService.AddNewAccount(registerAccount)) { //auto login and redirect based on role var loginAccount = new { Username = registerAccount.Username, RoleName = accountService.GetRoleName(registerAccount.Username), RoleId = registerAccount.RoleId, FullName = registerAccount.LastName + " " + registerAccount.FirstName }; Session["CURRENT_USER_ID"] = JsonConvert.SerializeObject(loginAccount, Formatting.Indented); bool resultMerge = shoppingService.MergeCartSessionAnddDDB(Session.GetCurrentUserInfo("Username")); if (resultMerge) //done => remove cart in session { Session["CART"] = null; } return(Redirect(Request.UrlReferrer.ToString())); } return(Content("Unexpected error")); } //return unexpected error please try again //will have a 404 not found page default for all error return(Content("Unexpected error")); }
public void AddNewAccount(Account newAccount) { _accountService.AddNewAccount(newAccount); }