I'm implementing a login to my website by simply authenticating the user and recording it's information in cookies. In order to protect pages that require registered users, what I'm doing is to call a method that reads the cookie and tries to authenticate the user again, so if the cookie is not expired and the user and pass inside of it is found in the db, the session goes on.
Since I'm calling this method in every action in the controller, I'm kind of unsure if what I'm doing is right or not, so my question is that is there a better way to secure the access to the controller without having to rewrite the same code over and over ?
Example of what I've done:
public ActionResult Act1 () {
if (CheckCookie() == true)
return View();
else
return RedirectToAction("Login","Home");
}
public ActionResult Act2 () {
if (CheckCookie() == true)
return View();
else
return RedirectToAction("Login","Home");
}
As you can see the same kind of code is repeated in each action in the way I'm implementing the login.