Monday, 6 April 2020

Create 'Validate Session Attribute' for validating session in MVC application.

Create 'Validate Session Attribute' for validating session  in MVC application.



using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace HRMS.UTILITIES
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class ValidateSessionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Session["UserId"] == null ||  HttpContext.Current.Session["DivisionId"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new

                               RouteValueDictionary(new { controller = "Login", action = "Login", area = "" }));
                return;
            }
            base.OnActionExecuting(filterContext);
        }
    }
}


USE:- [ValidateSession]  over controller or methods

1 comment: