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
code works. Keep udpating more and more AWS Online training
ReplyDeleteDot Net OnlineTraining
DevOps Online Training
Selenium Online Training