public IActionResult Register(RegisterViewModel user) { // var userList = _context.users.Where(p => p.email== user.email).FirstOrDefault(); // if(user.email == userList.email){ // ModelState.AddModelError("email", "email existed"); // } if (ModelState.IsValid) { PasswordHasher <RegisterViewModel> Hasher = new PasswordHasher <RegisterViewModel>(); user.password = Hasher.HashPassword(user, user.password); users User = new users() { first_name = user.first_name, last_name = user.last_name, email = user.email, password = user.password, created_at = DateTime.Now, updated_at = DateTime.Now }; _context.Add(User); _context.SaveChanges(); return(RedirectToAction("Wall")); } else { return(View("index")); } }
public IActionResult RegisterUser(RegisterUser newuser) { User CheckEmail = _wContext.users .Where(u => u.email == newuser.email) .SingleOrDefault(); if (CheckEmail != null) { ViewBag.errors = "That email already exists"; return(RedirectToAction("Register")); } if (ModelState.IsValid) { PasswordHasher <RegisterUser> Hasher = new PasswordHasher <RegisterUser>(); User newUser = new User { user_id = newuser.user_id, first_name = newuser.first_name, last_name = newuser.last_name, email = newuser.email, password = Hasher.HashPassword(newuser, newuser.password) }; _wContext.Add(newUser); _wContext.SaveChanges(); ViewBag.success = "Successfully registered"; return(RedirectToAction("Login")); } else { return(View("Register")); } }
public IActionResult PostMessage(Message message) { if (HttpContext.Session.GetInt32("UserId") != null) { Message NewMessage = new Message { MessageText = message.MessageText, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, UserId = (int)HttpContext.Session.GetInt32("UserId") }; _context.Messages.Add(NewMessage); _context.SaveChanges(); ViewBag.AllMessages = _context.Messages .Include(post => post.User) .OrderByDescending(post => post.CreatedAt) .Include(post => post.Comments) .ThenInclude(thisComment => thisComment.User) .ToList(); int?logId = HttpContext.Session.GetInt32("UserId"); ViewBag.LoggedUser = _context.Users.SingleOrDefault(user => user.UserId == logId); ModelState.Clear(); return(RedirectToAction("Index")); } else { return(RedirectToAction("Index", "Login")); } }
public IActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { User CheckUser = _context.Users.SingleOrDefault(u => u.Email == model.Email); if (CheckUser != null) { TempData["EmailInUseError"] = "Email Aleady in use"; return(View("Index")); } else { PasswordHasher <User> Hasher = new PasswordHasher <User>(); User user = new User() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Created_At = DateTime.Now, Updated_At = DateTime.Now }; user.Password = Hasher.HashPassword(user, model.Password); _context.Add(user); _context.SaveChanges(); HttpContext.Session.SetInt32("currentUserId", user.UserId); HttpContext.Session.SetString("currentFirstName", user.FirstName); return(RedirectToAction("Dashboard")); } } else { return(View("Index")); } }
public IActionResult register(IndexViewModel user) { User regUser = user.NewRegUser; // Check initial ModelState if (ModelState.IsValid) { // If a User exists with provided email if (dbContext.Users.Any(u => u.Email == regUser.Email)) { ModelState.AddModelError("regUser.Email", "Email already registered!"); return(RedirectToAction("Index")); } else { PasswordHasher <User> Hasher = new PasswordHasher <User>(); regUser.Password = Hasher.HashPassword(regUser, regUser.Password); // Add the user to database here dbContext.Add(regUser); dbContext.SaveChanges(); HttpContext.Session.SetString("UserEmail", regUser.Email); return(RedirectToAction("Dashboard")); } } else { return(View("Index")); } }
public IActionResult Register(NewUser newUser) { if (!ModelState.IsValid) { return(View("Index")); } else { if (dbContext.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); } else { PasswordHasher <NewUser> Hasher = new PasswordHasher <NewUser>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); User addUser = newUser.GetNewuser(); dbContext.Users.Add(addUser); dbContext.SaveChanges(); Loginuser.SetLogin(HttpContext, addUser.UserId); return(Redirect("Dashboard")); } } }
public IActionResult AddUser(Validations validator) { if (ModelState.IsValid) { PasswordHasher <Validations> Hasher = new PasswordHasher <Validations>(); validator.password = Hasher.HashPassword(validator, validator.password); Users myUser = new Users(); myUser.first_name = validator.first_name; myUser.last_name = validator.last_name; myUser.email = validator.email; myUser.password = validator.password; myUser.created_at = DateTime.Now; myUser.updated_at = DateTime.Now; _context.Add(myUser); _context.SaveChanges(); HttpContext.Session.SetInt32("UserID", myUser.id); int?UserID = HttpContext.Session.GetInt32("UserID"); ViewBag.UserID = UserID; return(RedirectToAction("Wall")); } else { return(View("Index")); } }
public IActionResult Register(RegUser _user) { HttpContext.Session.Clear(); if (ModelState.IsValid) { _context.Add(_user); _context.SaveChanges(); HttpContext.Session.SetInt32("active", _user.id); return(RedirectToAction("Wall", active)); } return(View("Index")); }
public IActionResult AddItem([FromBody] Item item) { if (item == null) { return(BadRequest()); } context.WallItems.Add(item); context.SaveChanges(); return(CreatedAtRoute("GetItem", new { id = item.Id }, item)); }
public IActionResult MessageMaker(string contents) { if (contents != null) { Message message = new Message(); message.Content = contents; message.UserId = (int)SessionId; database.Add(message); database.SaveChanges(); return(RedirectToAction("MessageWall")); } return(RedirectToAction("MessageWall")); }
public IActionResult messagepost(Message newMsg) { int? id = HttpContext.Session.GetInt32("CurrentUser"); Message NewMessage = new Message(); { NewMessage.Msg = newMsg.Msg; NewMessage.UserId = (int)id; dbContext.Add(NewMessage); dbContext.SaveChanges(); } return(RedirectToAction("WallView")); }
public IActionResult Message(string message) { int? currId = HttpContext.Session.GetInt32("currId"); Message newMess = new Message { message = message, userid = currId }; _context.Add(newMess); _context.SaveChanges(); return(RedirectToAction("Display")); }
public IActionResult reg(User user, string ConfirmPass) { if(ModelState.IsValid && ConfirmPass == user.Password) { PasswordHasher<User> Hasher = new PasswordHasher<User>(); user.Password = Hasher.HashPassword(user, user.Password); _context.users.Add(user); _context.SaveChanges(); return View("Message", User); } else { return View("Index"); } }
public IActionResult Register(UindexView patron) { if (ModelState.IsValid) { // check the email isn't already in the data base if (database.Users.Any(u => u.Email == patron.UserNew.Email)) { ModelState.AddModelError("UserNew.Email", "Email already in use... sorry"); Console.WriteLine("###### BAD EMAIL"); return(View("UserIndex", patron)); } // hash the password PasswordHasher <User> hasher = new PasswordHasher <User>(); string hashedPW = hasher .HashPassword(patron.UserNew, patron.UserNew.Password); patron.UserNew.Password = hashedPW; Console.WriteLine("###### PW HAS BEEN HASHED"); // Add and save to the database database.Add(patron.UserNew); database.SaveChanges(); SessionId = patron.UserNew.UserId; SessionName = patron.UserNew.FirstName; Console.WriteLine("###### VALID"); return(RedirectToAction("MessageWall", "Wall")); } Console.WriteLine("###### INVALID"); return(View("UserIndex", patron)); }
public IActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { User ExistingUser = _context.Users.SingleOrDefault(user => user.Email == model.Email); if (ExistingUser != null) { ViewBag.Message = "User with this email already exists!"; return(View("Index", model)); } User NewUser = new User { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Password = model.Password, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; _context.Add(NewUser); _context.SaveChanges(); NewUser = _context.Users.SingleOrDefault(user => user.Email == NewUser.Email); HttpContext.Session.SetInt32("UserId", NewUser.UserId); return(RedirectToAction("Index", "Messages")); } else { return(View("Index", model)); } }
public IActionResult Register(User NewUser) { if (ModelState.IsValid) { if (dbContext.users.Any(u => u.Email == NewUser.Email)) { ModelState.AddModelError("Email", "Email already in use!"); return(View("index")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); User newUser = new User { FirstName = NewUser.FirstName, LastName = NewUser.LastName, Email = NewUser.Email, Password = hasher.HashPassword(NewUser, NewUser.Password) }; dbContext.users.Add(newUser); dbContext.SaveChanges(); var currentuser = dbContext.users.FirstOrDefault(u => u.Email == NewUser.Email); HttpContext.Session.SetInt32("CurrentUser", currentuser.UserId); System.Console.WriteLine("########################"); System.Console.WriteLine(currentuser.UserId); return(RedirectToAction("WallView", "Wall")); } return(View("index")); }
public IActionResult Register(RegisterViewModel model) { User existingUser = _context.users.SingleOrDefault(user => user.email == model.email); if (ModelState.IsValid && existingUser == null) { User newUser = new User { first = model.first, last = model.last, email = model.email, password = model.password }; _context.users.Add(newUser); _context.SaveChanges(); User currUser = _context.users.SingleOrDefault(user => user.email == model.email); int currId = currUser.userid; HttpContext.Session.SetInt32("currId", currId); return(RedirectToAction("Display", "Wall")); } if (existingUser != null) { HttpContext.Session.SetString("existing", "Email has already been used"); return(RedirectToAction("Index")); } ViewBag.errors = ModelState.Values; return(View("Index")); }
public IActionResult MessageProcess(Messages mess) { mess.userId = (int)HttpContext.Session.GetInt32("id"); if (ModelState.IsValid) { dbContext.Add(mess); dbContext.SaveChanges(); return(RedirectToAction("Success")); } int?id = HttpContext.Session.GetInt32("id"); ViewBag.allMessage = dbContext.Messages .Include(y => y.User) .Include(u => u.Comments) .ThenInclude(u => u.User) .ToList(); ViewBag.User = dbContext.Users.FirstOrDefault(d => d.userId == id); return(View("Message")); }
public ActionResult Publish(string name, string text) { wallContext.Posts.Add(new Post { Name = name, Text = text }); wallContext.SaveChanges(); return(Json(new Post { Name = name, Text = text }, JsonRequestBehavior.AllowGet)); }
public IActionResult Register(User user) { if (ModelState.IsValid) { // Invalid Email // if (dbContext.Users.Any(u => u.Email == user.Email)) { ModelState.AddModelError("Email", "This Email is already in use!"); // return the view return(View("Index")); } // invalid Password // if (user.Password != user.Confirm) { // Manually add a ModelState error to the Email field, with provided // error message ModelState.AddModelError("Password", "Password confirmation was not the same password."); // return the view return(View("Index")); } // // // // // // // // // // // hash the given password: // Initialize the hasher object var hasher = new PasswordHasher <User>(); user.Password = hasher.HashPassword(user, user.Password); dbContext.Add(user); dbContext.SaveChanges(); HttpContext.Session.SetString("UserName", user.FirstName); HttpContext.Session.SetInt32("UserId", user.UserId); return(RedirectToAction("TheWall")); } else { return(View("Index")); } }
public WallController(WallContext wallContext) { context = wallContext; if (context.WallItems.Count() == 0) { context.WallItems.Add(new Item { Name = "first item" }); context.SaveChanges(); } }
public IActionResult Create(Users user) { if (ModelState.IsValid) { var emailCheck = _context.Users.SingleOrDefault(u => u.Email == user.Email); if (emailCheck == null) { PasswordHasher <Users> Hasher = new PasswordHasher <Users>(); user.Password = Hasher.HashPassword(user, user.Password); _context.Add(user); _context.SaveChanges(); HttpContext.Session.SetInt32("Id", user.UserId); HttpContext.Session.SetString("UserName", user.FirstName); return(RedirectToAction("Dashboard")); } ModelState.AddModelError("Email", "Email is already registered."); return(View("Register", user)); } else { return(View("Register", user)); } }
public IActionResult DeleteMessage(int messageId) { if (HttpContext.Session.GetInt32("curUser") == null) { return(RedirectToAction("Index")); } if (!dbContext.Messages.Any(u => u.MessageId == messageId)) { return(RedirectToAction("Index")); } Message selectedMessage = dbContext.Messages .Include(c => c.User) .FirstOrDefault(w => w.MessageId == messageId); int curUser = (int)HttpContext.Session.GetInt32("curUser"); if (selectedMessage.User.UserId != curUser) { return(RedirectToAction("Index")); } dbContext.Messages.Remove(selectedMessage); dbContext.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Index(string user, string text) { using (var context = new WallContext()) { context.Comments.Add(new Models.Comment() { Text = text, User = user }); context.SaveChanges(); ViewBag.Comments = context.Comments.ToList(); } return(Json(new { user, text })); }
public IActionResult Create(User user) { if (ModelState.IsValid) { // If a User exists with provided email if (dbContext.Users.Any(u => u.Email == user.Email)) { // Manually add a ModelState error to the Email field ModelState.AddModelError("Email", "Email already in use!"); return(View("Index")); } // hash password PasswordHasher <User> Hasher = new PasswordHasher <User>(); user.Password = Hasher.HashPassword(user, user.Password); // create user dbContext.Add(user); dbContext.SaveChanges(); // sign user into session var NewUser = dbContext.Users.FirstOrDefault(u => u.Email == user.Email); int UserId = NewUser.UserId; HttpContext.Session.SetInt32("UserId", UserId); HttpContext.Session.SetString("UserFirstName", user.FirstName); HttpContext.Session.SetString("UserLastName", user.LastName); // go to success return(RedirectToAction("Wall")); } // display errors else { return(View("Index")); } }
public async Task OnPostAsync_ShouldAddCommentToContext() { //Arrange var options = new DbContextOptionsBuilder <WallContext>() .UseInMemoryDatabase(databaseName: "CommentWallDataBase") .Options; using (var context = new WallContext(options)) { var post = new Post() { Body = "Test post" }; var reply = new Comment() { Body = "Test comment" }; context.Posts.Add(post); context.SaveChanges(); string repliersName = "Mr Test"; //Act CommentController commentController = new CommentController(context); /*commentController.RepliersName = repliersName; * commentModel.Post = post; * commentModel.Reply = reply; * await commentModel.OnPostAsync(1);*/ //Assert Assert.Equal(repliersName, context.People.Where(p => p.Name == repliersName).FirstOrDefault().Name); Assert.Equal(reply, context.People.Where(p => p.Name == repliersName).FirstOrDefault().Comments[0]); Assert.Equal(reply, context.Comments.Where(r => r.Body == reply.Body).FirstOrDefault()); var post1 = context.Posts.Where(x => x.Comments.Contains(reply)).FirstOrDefault(); Assert.Equal(reply, post1.Comments[0]); } }