I have an simple MVC app I want to check first the session as this action
public ActionResult Index()
{
if (Session["UserInfo"] == null)
{
return RedirectToAction("Login", "Users");
}
return View();
}
My question is about is there a way to enforce this check to all actions without do it manual for each action?

Amit MohantyPosted Aug 8, 2023, 6:08 AM
Yes, you can enforce this check for all actions in your MVC application without manually adding the session check to each action by using a custom action filter. Action filters are attributes that can be applied globally to controllers or actions, allowing you to perform common tasks like session checks in a centralized manner.
Create a new class for your custom action filter attribute. Let's call it SessionCheckAttribute.
Apply the custom attribute to your controller or actions where you want the session check to be enforced:
Cr BhargaviPosted Aug 8, 2023, 8:26 AM