예제 #1
0
        public ActionResult SignUp(User user)
        {
            if (ModelState.IsValid)
            {
                var userInDb = unitOfWork.Users.GetByCredentials(user.UserName);
                if (userInDb != null)
                {
                    ModelState.AddModelError("UserName", "User Name allready exist.");
                    return(View("LoginForm"));
                }

                var newUser = new User
                {
                    UserName  = user.UserName,
                    Password  = AuthUtils.GenerateBase64HashPassword(user.Password),
                    BirthDate = user.BirthDate
                };

                var cookie = AuthUtils.GenerateCookie(newUser);
                Response.Cookies.Add(cookie);

                unitOfWork.Users.Add(newUser);
                unitOfWork.Save();
                ModelState.Clear();
                return(RedirectToAction("Index", "Home"));
            }
            return(View());
        }
예제 #2
0
 public ActionResult SignIn(User user)
 {
     if (ModelState.IsValid)
     {
         var base64Password = AuthUtils.GenerateBase64HashPassword(user.Password);
         var userInDb       = unitOfWork.Users.GetByCredentials(user.UserName, base64Password);
         if (userInDb != null)
         {
             var cookie = AuthUtils.GenerateCookie(user);
             Response.Cookies.Add(cookie);
             return(RedirectToAction("Index", "Home"));
         }
         ViewBag.ErrorMessage = "Provided credentials are wrong.";
     }
     return(View("LoginForm", user));
 }