public bool DoesEmailExist(string email) { using (BoeingContext db = new BoeingContext()) { AppUser user = db.AppUsers.Where(a => a.UserEmail == email).FirstOrDefault(); return(user == null ? false : true); } }
public AppUser GetUser(string email) { using (BoeingContext db = new BoeingContext()) { AppUser user = db.AppUsers.Where(a => a.UserEmail == email).FirstOrDefault(); return(user); } }
public ToolEstimator(Estimate estimate, Statistic toolData, int toolTypeID) { this.estimate = estimate; this.toolData = toolData; this.toolTypeID = toolTypeID; baseHours = toolData.AverageHours; //number is managed in the admin portal releaseHours = toolData.ReleaseHours; //default release hours is equal to toolData.ReleaseHours. Can be changed in admin portal db = new BoeingContext(); }
public ActionResult Register(RegisterModel model) { AppUser appuser = new AppUser(); //Model Validation if (ModelState.IsValid) { //Will be true if (1) the email address already exists in the DB and (2) the user hasn't been deleted bool userWasDeleted = false; #region//Does the email already exist appuser.UserEmail = model.UserEmail; bool doesExist = DoesEmailExist(appuser.UserEmail); if (doesExist) { AppUser existingUser = GetUser(appuser.UserEmail); //User exists and is active if (!existingUser.IsDeleted) { ModelState.AddModelError("EmailExist", "User already exists."); return(View()); } //User exists and is not active else { userWasDeleted = true; //Make the appuser into the existing active user and override info appuser = existingUser; } } #endregion appuser.FirstName = model.FirstName; appuser.LastName = model.LastName; #region//Password Salting and Hashing appuser.PasswordSalt = Crypto.GenerateSalt(); var password = model.PasswordHash + appuser.PasswordSalt; appuser.PasswordHash = Crypto.SHA256(password); #endregion //default access level is 2, or a standard user #region//AccessLevelID appuser.AccessLevelID = 2; #endregion #region//Save data to databese using (BoeingContext db = new BoeingContext()) { //Do not add an existing user into the DB if (!userWasDeleted) { appuser.CreatedDate = DateTime.Today; db.AppUsers.Add(appuser); } //The user already exists, but now is modified else { //Set user to existant (because they're being registered) appuser.IsDeleted = false; db.Entry(appuser).State = EntityState.Modified; } try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } } #endregion //Creates an authentication ticket for the supplied user name and adds it to the cookies collection of the response, or to the URL if you are using cookieless authentication. FormsAuthentication.SetAuthCookie(appuser.UserEmail, true); //true to keep logged in } else { var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception)); } return(RedirectToAction("Index", "Home")); }