コード例 #1
0
 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"));
     }
 }
コード例 #2
0
        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"));
            }
        }
コード例 #3
0
 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"));
     }
 }
コード例 #4
0
ファイル: HomeController.cs プロジェクト: wncowan/TheWall
 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"));
     }
 }
コード例 #5
0
ファイル: HomeController.cs プロジェクト: devsoor/CSharp
        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"));
            }
        }
コード例 #6
0
 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"));
         }
     }
 }
コード例 #7
0
        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"));
            }
        }
コード例 #8
0
 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"));
 }
コード例 #9
0
        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));
        }
コード例 #10
0
 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"));
 }
コード例 #11
0
        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"));
        }
コード例 #12
0
ファイル: WallController.cs プロジェクト: henryvu25/wall
        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"));
        }
コード例 #13
0
 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");
     }
 }
コード例 #14
0
 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));
 }
コード例 #15
0
 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));
     }
 }
コード例 #16
0
        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"));
        }
コード例 #17
0
        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"));
        }
コード例 #18
0
        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"));
        }
コード例 #19
0
        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));
        }
コード例 #20
0
        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"));
            }
        }
コード例 #21
0
        public WallController(WallContext wallContext)
        {
            context = wallContext;

            if (context.WallItems.Count() == 0)
            {
                context.WallItems.Add(new Item {
                    Name = "first item"
                });
                context.SaveChanges();
            }
        }
コード例 #22
0
 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));
     }
 }
コード例 #23
0
        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"));
        }
コード例 #24
0
        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 }));
        }
コード例 #25
0
        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"));
            }
        }
コード例 #26
0
        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]);
            }
        }