public ActionResult Register() { // Checks if user has already logged in if (Session["CustomerId"] == null) { // Create lists that can be rendered in the drop down list var model = new CustomerRegistrationData(); model.Provinces = Lists.getAllProvinces(); model.Countries = Lists.getAllCountries(); // If not logged in, proceed to registration return(View(model)); } else { // If logged in, redirect to dashboard return(RedirectToAction("Index", "Dashboard")); } }
public ActionResult Register(CustomerRegistrationData cust) { // Need to recreate the list for the drop down list again, in case the page is reloaded cust.Provinces = Lists.getAllProvinces(); cust.Countries = Lists.getAllCountries(); if (ModelState.IsValid) { // Check if username is already in use var usernameExists = UsernameTaken(cust.Username); if (usernameExists) // username is taken { ModelState.AddModelError("UsernameExist", "That username is taken"); return(View(cust)); } // Check if email is already in use var emailExists = EmailTaken(cust.CustEmail); if (emailExists) // email is taken and not null { ModelState.AddModelError("EmailExist", "That email is already registered"); return(View(cust)); } // Password hashing - salt isn't working //cust.Salt = Crypto.CreateSalt(10); //cust.UserPassword = Crypto.Hash(cust.UserPassword, cust.Salt); //cust.ConfirmPassword = Crypto.Hash(cust.ConfirmPassword, cust.Salt); cust.UserPassword = Crypto.HashNoSalt(cust.UserPassword); cust.ConfirmPassword = Crypto.HashNoSalt(cust.ConfirmPassword); // Convert enumerator values for Province and Country to string values for DB entry // Save customer login info into RegisteredUsers table in the database using (db) { // Save all customer info in CustomerRegistrationData table in the database db.CustomerRegistrationDatas.Add(cust); // Save customer personal info in Customers table in the database Customer cst = new Customer(); cst.CustFirstName = cust.CustFirstName; cst.CustLastName = cust.CustLastName; cst.CustAddress = cust.CustAddress; cst.CustCity = cust.CustCity; cst.CustProv = cust.CustProv; cst.CustPostal = cust.CustPostal; cst.CustCountry = cust.CustCountry; cst.CustHomePhone = cust.CustHomePhone; cst.CustBusPhone = cust.CustBusPhone; cst.CustEmail = cust.CustEmail; db.Customers.Add(cst); // Save customer login info in RegisteredUsers table in the database RegisteredUser user = new RegisteredUser(); user.Username = cust.Username; user.Salt = cust.Salt; user.UserPassword = cust.UserPassword; db.RegisteredUsers.Add(user); // Save all database changes try { db.SaveChanges(); // Clear ModelState for future registration ModelState.Clear(); cust = null; user = null; TempData["AcctMessage"] = "Account registration was successful!"; } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"", ve.PropertyName, eve.Entry.CurrentValues.GetValue <object>(ve.PropertyName), ve.ErrorMessage); } } throw; } catch (Exception ex) { throw ex; } } return(RedirectToAction("Login", "Customer")); } else // invalid data, make user re-enter info { return(View(cust)); } }