// GET: Users/Edit/5 public async Task <ActionResult> Edit(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } User user = await db.Users.FindAsync(id); if (user == null) { return(HttpNotFound()); } var EdituserViewModel = new EditUserViewModel { Age = user.Age, Date = user.Date, Country = user.Country, Hobby = user.Hobby, UserID = user.UserID, UserName = user.UserName, Password = PasswordEncryption.TextToEncrypt(user.Password) }; return(View(EdituserViewModel)); }
public ActionResult Authorize(UserViewModel userModel) { if (!ModelState.IsValid) { return(View("Index", null)); } //To authorize the username and the password we will use the database entities class to query using (LoginDataBaseEntities db = new LoginDataBaseEntities()) { var hashPass = PasswordEncryption.TextToEncrypt(userModel.Password); User userDetails = db.Users.Where(x => x.UserName == userModel.UserName && x.Password == hashPass).SingleOrDefault(); //inside this if statement we handeled the situation when there is wrong username or password if (userDetails == null) { //userModel.LoginErrorMessage = "Wrong Username or Password"; return(View("Index", userModel)); } else { //after the login session is successfull we saved the userID, we can also save the username Session["userID"] = userDetails.UserID; Session["userName"] = userDetails.UserName; return(RedirectToAction("Index", "Home")); } } }
public async Task <ActionResult> Create([Bind(Include = "UserID,UserName,Password,Age,Hobby,Country,Date")] User user) { if (ModelState.IsValid) { user.UserID = Guid.NewGuid(); user.Password = PasswordEncryption.TextToEncrypt(user.Password); db.Users.Add(user); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(user)); }
public async Task <ActionResult> Edit(EditUserViewModel UserViewModel) { var user = new User { Age = UserViewModel.Age, Date = UserViewModel.Date, Country = UserViewModel.Country, Hobby = UserViewModel.Hobby, UserID = UserViewModel.UserID, UserName = UserViewModel.UserName, Password = PasswordEncryption.TextToEncrypt(UserViewModel.Password) }; if (ModelState.IsValid) { db.Entry(user).State = EntityState.Modified; try { await db.SaveChangesAsync(); } 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}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } } return(RedirectToAction("Index")); } return(View(UserViewModel)); }