public ActionResult <DTO.User> Edit(int id) { if (User.Claims.FirstOrDefault(c => c.Type == "Id").Value != id.ToString()) { ((Controller)this).DisplayTempData("warning", "You are not supposed to edit other user's data!"); return(Redirect("~/")); } using (BenFattoAppContext context = new BenFattoAppContext()) { BenFattoUser loggedUser = context.Users.FirstOrDefault(u => u.Id == id); if (null == loggedUser) { ((Controller)this).DisplayTempData("warning", "Seems we can't find your data!"); return(Redirect("~/")); } return(View(new DTO.User { Id = loggedUser.Id, Name = loggedUser.Name, Email = loggedUser.Email, Password = loggedUser.Password })); } }
public IActionResult Login([FromForm] BenFattoUser user) { try { using (BenFattoAppContext context = new BenFattoAppContext()) { BenFattoUser loggingInUser = context.Users.FirstOrDefault(u => u.Email.ToLower() == user.Email.ToLower() && u.Password == user.Password ); if (null == loggingInUser) { return(Forbid()); } SignUserIn(loggingInUser); } messageType = "info"; message = $"You have successfuly logged into this application!"; } catch (Exception ex) { messageType = "error"; message = $"Something went wrong: {ex.Message}"; } finally { ((Controller)this).DisplayTempData(messageType, message); } return(Redirect("~/")); }
public ActionResult <IEnumerable <Model.BenFattoUser> > Index() { List <Model.BenFattoUser> users; using (BenFattoAppContext context = new BenFattoAppContext()) { users = context.Users.ToList(); } return(View(users)); }
public ActionResult <Model.BenFattoUser> Edit([FromForm] DTO.User user) { try { using (BenFattoAppContext context = new BenFattoAppContext()) { BenFattoUser loggedUser = context.Users.FirstOrDefault(u => u.Id == user.Id); if (null == loggedUser) { ((Controller)this).DisplayTempData("warning", "Oooops! Seems like your user is not available! Please verify"); HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return(Redirect("~/")); } if (loggedUser.Password != user.Password) { ((Controller)this).DisplayTempData("warning", "Your password does not match! please try again!"); return(Redirect(Request.Headers["Referer"].ToString())); } loggedUser.Password = user.NewPassword; loggedUser.Name = user.Name; loggedUser.Email = user.Email; context.Entry(loggedUser).State = EntityState.Modified; context.SaveChanges(); HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); SignUserIn(loggedUser); } messageType = "success"; message = $"You have successfully updated your data!"; } catch (Exception ex) { messageType = "error"; message = $"Something went wrong: {ex.Message}"; } finally { ((Controller)this).DisplayTempData(messageType, message); } return(Redirect(Request.Headers["Referer"].ToString())); }
public IActionResult Create([FromForm] BenFattoUser user) { try { using (BenFattoAppContext context = new BenFattoAppContext()) { context.Users.Add(user); context.SaveChanges(); } messageType = "success"; message = $"User {user.Name} created successfully!"; } catch (Exception ex) { messageType = "error"; message = $"Something went wrong: {ex.Message}"; } finally { ((Controller)this).DisplayTempData(messageType, message); } return(Redirect(Request.Headers["Referer"].ToString())); }
private static void CreateDefaulUser() { WriteOut("OK! Let's create a user!"); WriteOut("Enter \"#CANCEL!\" anytime to cancel this operation!"); WriteOut("Enter the new user's name (Name + Last Name)!"); string userName = Console.ReadLine(); if ("#CANCEL!" == userName) { return; } WriteOut($"Enter {userName}'s e-mail address!"); string email = Console.ReadLine(); if ("#CANCEL!" == email) { return; } WriteOut("Finally enter the password!"); string password = Console.ReadLine(); if ("#CANCEL!" == password) { return; } BenFattoUser usr = new BenFattoUser { Email = email, Name = userName, Password = password }; using (BenFattoAppContext context = new BenFattoAppContext()) { context.Users.Add(usr); context.SaveChanges(); } WriteOut($"User successfuly created with Id: {usr.Id}"); }