Create filter to redirect page to login page on direct hit URL in MVC.
Steps:
1. Add a new folder Name it Filter.
2. Add a new class name it NoDirectAccessAttribute.
3. Inherit ActionFilterAttribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace DotNetByPriyanshu.Filter
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class NoDirectAccessAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.UrlReferrer == null ||
filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new { controller = "Login", action = "Login", area = "" }));
}
}
}
}
Uses:- use as an attribute over all Controller.
like:- [NoDirectAccessAttribute]
No comments:
Post a Comment