Ejemplo n.º 1
0
        public IActionResult Register(User user, string PasswordConfirm)
        {
            if (user.Password != PasswordConfirm)
            {
                ViewBag.PasswordError = "Passwords don't match";
                return(View("Index"));
            }
            if (ModelState.IsValid)
            {
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                user.Password = Hasher.HashPassword(user, user.Password);
                User ExistingEmail = _context.users.SingleOrDefault(u => u.Email == user.Email);
                if (ExistingEmail != null)
                {
                    ViewBag.RegError = "Email already registered";
                    return(View("Index"));
                }
                else
                {
                    _context.Add(user);
                    _context.SaveChanges();

                    int?UserId = user.UserId;
                    ViewBag.CurrentUser = user;
                    HttpContext.Session.SetInt32("UserId", (int)UserId);
                    HttpContext.Session.SetString("UserEmail", (string)user.Email);
                    HttpContext.Session.SetString("UserName", (string)user.Name);
                    return(RedirectToAction("Profile"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }