예제 #1
0
        public ActionResult Login(User user)
        {
            try
            {
                string login    = user.Login;
                string password = user.Password;

                Author author = authorRepository.FindByLogin(login);
                if (author != null)
                {
                    if (author.Password == password)
                    {
                        HttpContext.Session.Set("userId", BitConverter.GetBytes(author.Id));
                        HttpContext.Session.Set("isAuth", BitConverter.GetBytes(true));
                        ViewBag.HasErrors = false;
                        return(RedirectToAction("AuthorIndex", "Course"));
                    }
                }
                Subscriber subscriber = subscriberRepository.FindByLogin(login);
                if (subscriber != null)
                {
                    if (subscriber.Password == password)
                    {
                        HttpContext.Session.Set("userId", BitConverter.GetBytes(subscriber.Id));
                        HttpContext.Session.Set("isAuth", BitConverter.GetBytes(false));
                        ViewBag.HasErrors = false;
                        return(RedirectToAction("SubscriberIndex", "Course"));
                    }
                }
                ViewBag.HasErrors  = true;
                ViewBag.LoginError = "Login or password is incorrect";

                return(View(user));
            }
            catch
            {
                ViewBag.HasErrors  = true;
                ViewBag.LoginError = "Something went wrong. Please contact your system administrator";
                return(View(user));
            }
        }