public async Task <ActionResult> ManageUser(USER user) { ViewBag.ReturnUrl = Url.Action("ManageUser"); // Remove the useles data column because we dont#t need them to Change the password ModelState.Remove("password"); ModelState.Remove("userName"); ModelState.Remove("userID"); ModelState.Remove("email"); int userId = user.userID; // var errors3 = ModelState //.Where(x => x.Value.Errors.Count > 0) //.Select(x => new { x.Key, x.Value.Errors }) //.ToArray(); if (ModelState.IsValid) { // Set obligated User Property before updated the changes user.password = user.newPassword; user.userName = User.Identity.GetUserName().Split('|')[0].ToString(); user.userID = Int32.Parse(User.Identity.GetUserName().Split('|')[1]); using (FirstCargoDbEntities entities = new FirstCargoDbEntities()) { USER userToUpdate = entities.USER.SingleOrDefault(u => u.userName == user.userName); var hashCode = userToUpdate.vCode; //Password Hasing Process Call Helper Class Method var encodingPasswordString = RegistrationLoginHelper.EncodePassword(user.oldPassword, hashCode); if (encodingPasswordString.Equals(userToUpdate.password)) { //Check Login Detail User Name Or Password var query = (from s in entities.USER where (s.userName == user.userName || s.email == user.userName) && s.password.Equals(encodingPasswordString) select s).FirstOrDefault(); if (query != null) { var password = RegistrationLoginHelper.EncodePassword(user.newPassword, hashCode); userToUpdate.oldPassword = userToUpdate.password; userToUpdate.password = userToUpdate.newPassword = userToUpdate.confirmPassword = password; userToUpdate.passwordChangedDates = DateTime.Now; db.Entry(userToUpdate).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { // Todo Log the error } } } } return(RedirectToAction("ManageUser", new { Message = NotificationMessage.ManageMessageId.ChangePasswordSuccess })); } // how form again if there is a failure return(View(user)); }
public ActionResult Login([Bind(Exclude = "oldPassword,confirmPassword")] USER model) { ModelState.Remove("oldPassword"); ModelState.Remove("confirmPassword"); ModelState.Remove("newPassword"); ModelState.Remove("email"); // Lets first check if the Model is valid or not if (ModelState.IsValid) { using (FirstCargoDbEntities entities = new FirstCargoDbEntities()) { string username = model.userName; string password = model.password; USER user = entities.USER.SingleOrDefault(u => u.userName == username); var hashCode = user.vCode; //Password Hasing Process Call Helper Class Method var encodingPasswordString = RegistrationLoginHelper.EncodePassword(password, hashCode); //Check Login Detail User Name Or Password var query = (from s in entities.USER where (s.userName == model.userName || s.email == model.userName) && s.password.Equals(encodingPasswordString) select s).FirstOrDefault(); // User found in the database if (query != null) { FormsAuthentication.SetAuthCookie(username + "|" + user.userID.ToString() + "|" + user.isAdmin, false); int test = CurrentUserId; string test2 = User.Identity.GetUserName().Split('|')[0]; string test3 = User.Identity.GetUserName(); return(RedirectToAction("Index", "Vehicule")); } else { ModelState.AddModelError("", @ViewResources.Resource.LoginError); } } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Create([Bind(Include = "userName,userFirstName,userLastName,dateOfBirth,password,isAdmin,email")] USER user) { // Remove the useles data column because we dont#t need them to Change the password ModelState.Remove("oldPassword"); ModelState.Remove("confirmPassword"); ModelState.Remove("newPassword"); var errors3 = ModelState .Where(x => x.Value.Errors.Count > 0) .Select(x => new { x.Key, x.Value.Errors }) .ToArray(); // This has to be remove later if (ModelState.IsValid) { try { var chkUser = (from s in db.USER where s.userName == user.userName || s.email == user.email select s).FirstOrDefault(); if (chkUser == null) { var keyNew = RegistrationLoginHelper.GeneratePassword(10); var password = RegistrationLoginHelper.EncodePassword(user.password, keyNew); user.password = password; user.createdDate = DateTime.Now; user.vCode = keyNew; //because they donot appear to the view we need to initiliaze them // Set obligated User Property before updated the changes user.oldPassword = user.password; user.newPassword = user.password; user.confirmPassword = user.password; user.createdDate = DateTime.Now; user.confirmationToken = ""; user.isConfirmed = false; db.USER.Add(user); await db.SaveChangesAsync(); //return RedirectToAction("LogIn", "Login"); } else { return(RedirectToAction("Index", new { Message = NotificationMessage.ManageMessageId.Error })); } } 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; //Todo Log the exception } return(RedirectToAction("Index", new { Message = NotificationMessage.ManageMessageId.RecordSuccess })); } return(View(user)); }