Override Authorize Filters in MVC
Override
Authorize Filters in MVC
Authorize Filter: It
is to limit the access for a user or user specific to the role.
Implementation
of Override Authorize filters in MVC
Step1: Add a controller and named it as HomeController
Add [Authorize]
attribute before an action or a controller based on the requirement. Suppose
user want to restrict the access of particular controller but allow some of
action of that controller.
So just add [Authorize]
before controller
using System;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
namespace WebApplication1.Controllers
{
[Authorize]
public class HomeController: Controller
{
}
}
Step 2: Create a class and
named it as OverrideAuthorizeAttribute
which should inherit AuthorizeAttribute and
Implements IOverrideFilter
public class OverrideAuthorizeAttribute: AuthorizeAttribute, IOverrideFilter
{
public Type
FiltersToOverride
{
get {
return typeof(IAuthorizationFilter);
}
}
}
Step 3: add two methods Index and Index1 in Home
Controller
using System;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
namespace WebApplication1.Controllers
{
[Authorize]
public class HomeController: Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult Index1()
{
return View();
}
}
}
Step 4: Write the [OverrideAuthorization] attribute before the method that u want to access.
[OverrideAuthorization]
public ActionResult Index1()
{
return View();
}
Step 5:
Run the Application & see the output
As we provide [Authorize] keyword before controller so all the method except index1 cannot be accessed. It will give the message unauthorized.
Let build and execute the index1, we will get the below output, because we have Override the Authorization Filter.
Comments
Post a Comment