public ActionResult Create(User model) { // Make sure the user is logged in and that they have permission if (!IsUserLoggedIn) { return(RedirectToLogin()); } if (!UserHasPermission(PermissionName.User)) { return(RedirectToPermissionDenied()); } // Make sure the entered data is valid if (ModelState.IsValid) { // Generate a password salt // Hash the password string passwordSalt = UserLogin.CreatePasswordSalt(); string password = UserLogin.HashPassword(model.Password, passwordSalt); // Clear password info from model, just in case for security model.Password = null; model.ConfirmPassword = null; // Create the user within the database try { CreateUser( model.Username, model.FirstName, model.LastName, model.EmailAddress, model.PhoneNumber, password, passwordSalt); return(RedirectToAction("Index")); } catch (Exception e) { ModelState.AddModelError("", e.Message); } } else { //show error var errors = ModelState.Values.SelectMany(v => v.Errors); } return(View()); }
public ActionResult CreateBulk(HttpPostedFileBase file) { // Make sure the user is logged in and that they have permission if (!IsUserLoggedIn) { return(RedirectToLogin()); } if (!UserHasPermission(PermissionName.User)) { return(RedirectToPermissionDenied()); } // Create data which needs to be outside the try-ctach block FileCSV data = null; int uploadCount = 0; int failCount = 0; Downloadable errorFile = null; // Enter a try-catch block to make sure any exceptions are caught try { // Decode the CSV file data = new FileCSV(file); // Make sure the headers are correct // This will throw an exception if not data.ValidateHeaders(new string[] { "Username", // 0 "FirstName", // 1 "LastName", // 2 "Email", // 3 "PhoneNo", // 4 "Password" // 5 }); // Loop through each row of data // Generate the list of results foreach (string[] row in data.Row) { // Generate a password salt // Hash the password string passwordSalt = UserLogin.CreatePasswordSalt(); string password = UserLogin.HashPassword(row[5], passwordSalt); // Create the user within the database try { CreateUser( row[0], // Username row[1], // FirstName row[2], // LastName row[3], // Email Convert.ToInt32(row[4]), // PhoneNo password, passwordSalt); data.SetComment(row, ""); uploadCount++; } catch (Exception e) { data.SetComment(row, e.Message); failCount++; } } // Generate and record the error file, if required if (failCount > 0) { errorFile = Downloadable.CreateCSV(data.GenerateErrorFile(), "errors.csv"); } } catch (Exception e) { // Record error message for View TempData["UploadError"] = e.Message; } // Record item counts for View if (uploadCount > 0) { TempData["UploadCount"] = uploadCount; } if (failCount > 0) { TempData["FailCount"] = failCount; } Session[FileCSV.SessionLabelUploadErrorLog] = (failCount > 0) ? errorFile : null; // All file processing has been completed // Go to the normal create page return(RedirectToAction("Create", "User")); }