public ActionResult Index()
 {
     if (User.Identity.IsAuthenticated)
     {
         business = new SportsChatBusiness();
         User u = GetAuthenticatedUser();
         if (u != null)
         {
             ViewBag.FirstName = u.FirstName;
         }
     }
     return View();
 }
 public User GetAuthenticatedUser()
 {
     if (User.Identity.IsAuthenticated)
     {
         SportsChatBusiness business = new SportsChatBusiness();
         User u = business.GetUserWithUserName(User.Identity.Name);
         if (u != null)
         {
            return u;
         }
         else
         {
             FormsAuthentication.SignOut();
             RedirectToAction("Login", ACCOUNT_CONTROLLER);
         }
     }
     RedirectToAction("Login", ACCOUNT_CONTROLLER);
     return null;
 }
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                SportsChatBusiness business = new SportsChatBusiness();
                string username = model.UserName;
                string password = model.Password;

                User u = null;
                try
                {
                    u = business.Authenticate(username, password);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }

                //user was found in the db
                if (u != null)
                {
                    u.DateLastLogin = DateTime.Now;
                    business.UpdateUser(u);
                    FormsAuthentication.SetAuthCookie(username, true);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
            return View(model);
        }
 public void UpdateSessionMessagesToLatest(int chatId)
 {
     SportsChatBusiness business = new SportsChatBusiness();
     Session[MESSAGE_LIST_SESSION] = business.GetAllMessagesForChat(chatId);
 }
        public ActionResult Register(RegisterViewModel model)
        {
            business = new SportsChatBusiness();
            if (model == null)
            {
                return View();
            }

            bool hasModelError = false;
            if (string.IsNullOrWhiteSpace(model.UserName))
            {
                ModelState.AddModelError("UserName", "Please enter a valid username");
                hasModelError = true;
            }
            if (string.IsNullOrWhiteSpace(model.Password))
            {
                ModelState.AddModelError("Password", "Please enter a valid password");
                hasModelError = true;
            }
            if (model.Password.Length < 9)
            {
                ModelState.AddModelError("Password", "Password must be at least 8 characters long");
                hasModelError = true;
            }
            if (string.IsNullOrWhiteSpace(model.FirstName))
            {
                ModelState.AddModelError("FirstName", "Please enter your first name");
                hasModelError = true;
            }
            if (string.IsNullOrWhiteSpace(model.LastName))
            {
                ModelState.AddModelError("LastName", "Please enter your last name");
                hasModelError = true;
            }
            if (!business.IsValidEmail(model.Email))
            {
                ModelState.AddModelError("Password", "Please enter a valid email address");
                hasModelError = true;
            }
            if (hasModelError)
            {
                model.hasErrors = true;
                return View(model);
            }

            //Once the data has been validated, add the user to the db
            try
            {
                int userid = business.CreateNewUser(model.UserName, model.Password, model.FirstName, model.LastName, model.Email);
                if (userid != null)
                {
                    User u = business.GetUserWithId(userid);
                    return View("RegisterConfirm", u);
                }
            }
            catch (InvalidUserNameException ex)
            {
                ModelState.AddModelError("UserName", "Username is already taken, please choose another");
                model.hasErrors = true;
                return View(model);
            }

            return View();
        }