Ejemplo n.º 1
0
        /// <summary>
        /// The login form for the admin site. Will auto redirect if logged in.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string email, string password)
        {
            if (db.Admins.Count() == 0)
            {
                db.Admins.Add(new AdminUser()
                {
                    Email = "*****@*****.**", FirstName = "Ali", LastName = "Khatami", Password = "******"
                });
                db.SaveChanges();
            }

            bool bAttemptedLogin = !string.IsNullOrEmpty(email) || !string.IsNullOrEmpty(password);

            // create the user instace
            AdminUser userFromCredentials = null;

            // try to find the user from credentials
            if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password))
            {
                userFromCredentials = db.Admins.FirstOrDefault(user => user.Email.ToLower() == email.ToLower() && user.Password == password);
            }

            // check if user is logged in as an admin already
            if (UserUtils.CurrentUser != null || userFromCredentials != null)
            {
                // create a session cookie for the user then redirect them
                UserUtils.CreateEncryptedUserCookie((userFromCredentials != null) ? userFromCredentials.ID : UserUtils.CurrentUser.ID);

                // redirect to the events page which is the first link in the navigation
                Response.Redirect("~/Admin/Events");
            }

            // if we didn't redirect it means someone unsuccessfully tried to login
            if (bAttemptedLogin)
            {
                ViewBag.FailedLogin = true;
            }

            return(View());
        }