views:

84

answers:

1

I'm trying to change the method OnAuthorization, so that it is available for any application ... this way:

public partial class Controller
{
    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        if ((string)(filterContext.RouteData.Values["action"]) == "test")
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }

}

but is showing the compilation error: ...Controller.OnAuthorization(System.Web.Mvc.AuthorizationContext)': no suitable method found to override

can someone help me?

A: 

You have to create you own base controller class:

public partial class BaseController : Controller
{
    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        if ((string)(filterContext.RouteData.Values["action"]) == "test")
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }
}

Use BaseController in your code.

And remember that filterContext.RouteData.Values["action"] can be Test or TEST or tEST.

LukLed