Пример #1
0
 public IActionResult RegisterProcess(UserValidator newuser)
 {
     if (ModelState.IsValid)
     {
         User DBUser = _context.users.SingleOrDefault(u => u.Email == newuser.Email);
         if (DBUser != null)
         {
             ViewBag.Error = "Email already exists in Database";
             return(View("Index"));
         }
         PasswordHasher <UserValidator> Hasher = new PasswordHasher <UserValidator>();
         newuser.Password = Hasher.HashPassword(newuser, newuser.Password);
         User this_user = new User
         {
             FirstName = newuser.FirstName,
             LastName  = newuser.LastName,
             Email     = newuser.Email,
             Password  = newuser.Password,
         };
         _context.Add(this_user);
         _context.SaveChanges();
         HttpContext.Session.SetInt32("UserId", this_user.UserId);
         HttpContext.Session.SetString("UserFirstName", this_user.FirstName);
         return(RedirectToAction("RecentSecrets"));
     }
     else
     {
         return(View("Index"));
     }
 }
Пример #2
0
 public IActionResult Create(Secret newSecret)
 {
     if (ModelState.IsValid)
     {
         dbContext.Secrets.Add(newSecret);
         dbContext.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View("Index", DashboardModel));
 }
Пример #3
0
        public IActionResult Register(User user)
        {
            if (ModelState.IsValid)
            {
                // check for uniqueness of email!
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    ModelState.AddModelError("Email", "Email already in use.");
                    return(View("Index"));
                }
                // hash pw
                PasswordHasher <User> hasher = new PasswordHasher <User>();
                string hashedPw = hasher.HashPassword(user, user.Password);

                user.Password = hashedPw;

                dbContext.Add(user);
                // once we save changes...
                // user.UserId will be updated!
                dbContext.SaveChanges();

                HttpContext.Session.SetInt32("UserId", user.UserId);

                return(RedirectToAction("Index", "Secret"));
            }
            return(View("Index"));
        }