public ActionResult SignUp(User user, string inviteCode) { if (user != null) { if (ModelState.IsValid) { // Generate salt and salted/hashed password for db storage string salt = StringManipulator.GenerateSalt(); string hashedPassword = StringManipulator.GenerateHashedPassword(salt, user.Password); // Set user properties user.Password = hashedPassword; user.Salt = salt; user.Role = "Standard"; user.Position = "N/A"; user.EmployeeID = user.FirstName[0] + user.LastName[0] + StringManipulator.GenerateIdNumber(8); user.RegDate = DateTime.Now; // If an invite code was present, join that org. If not, create a new one if (string.IsNullOrEmpty(inviteCode)) { Organization org = new Organization(); // Set Organization properties org.Label = user.Organization.Label; org.Registered = DateTime.Now; org.CodesCount = 1; org.OrganizationID = org.Label + "#" + StringManipulator.GenerateIdNumber(8); // Add new org to database db.Organizations.Add(org); db.SaveChanges(); // Link the user to the newly created org user.Organization = org; user.OrganizationID = org.Id; } else { // Find the organization relating to the invite code InviteCode code = db.InviteCodes.FirstOrDefault(i => i.Code == inviteCode); // If the code is valid, if (code != null && !code.IsExpired) { user.OrganizationID = code.OrganizationID; code.IsExpired = true; code.DateExpired = DateTime.Now; // Commit invite code changes db.SaveChanges(); } } // Commit user changes db.Users.Add(user); db.SaveChanges(); // Log the user creation event EventLogger.LogNewEvent(user.Id, user.OrganizationID, LoggingEventType.UserCreated, ""); } } return(View("Login")); }