//[ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User user = db.Users.FirstOrDefault(u => u.Email == model.Email);

                if (user == null)
                {
                    int guestId = db.Roles.Where(g => g.RoleName == "User").FirstOrDefault().Id;
                    db.Users.Add(new User
                    {
                        Email        = model.Email,
                        PasswordHash = RegisterModel.CreateHash(model.PasswordHash),
                        UserName     = model.UserName,
                        PhoneNumber  = model.PhoneNumber,
                        RoleId       = guestId
                    });
                    db.SaveChanges();

                    string checkPass = RegisterModel.CreateHash(model.PasswordHash);
                    user = db.Users.Where(u => u.Email == model.Email && u.PasswordHash == checkPass).FirstOrDefault();

                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Email, true);
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Ошибка! Пользователь с таким email уже существует!");
                    }
                }
            }
            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult Create([Bind(Include = "Id,Email,PasswordHash,UserName,PhoneNumber,RoleId")] User user)
        {
            if (ModelState.IsValid)
            {
                user.PasswordHash = RegisterModel.CreateHash(user.PasswordHash);
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.RoleId = new SelectList(db.Roles, "Id", "RoleName", user.RoleId);
            return(View(user));
        }
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                string checkPass = RegisterModel.CreateHash(model.PasswordHash);
                User   user      = db.Users.FirstOrDefault(u => u.Email == model.Email && u.PasswordHash == checkPass);

                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(model.Email, true);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Ошибка! Пользователь с таким email и паролем не найден!");
                }
            }

            return(View(model));
        }