asp.net mvc RequireLocalHostActionFilter not fireing
Im trying to implement the RequireLocalHostActionFilter that Phil Haack did in one of his shows.
Its the ActionFilter thats checks if a call to a giving method is from the local host, and is registred in the global filters.
But my filter is not working, and I cant get my head around it.
So please, if someone has some free time to take a look.
My ActionFilter:
public class RequireLocalHostActionFilter : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return !httpContext.Request.IsLocal; // I need to test on the local host, so I reverse the logic.
}
}
My FilterProvider
public class ConditionalFilterProvider : IFilterProvider
{
public readonly IEnumerable<Func<ControllerContext, ActionDescriptor, object>> _conditions;
public ConditionalFilterProvider(IEnumerable<Func<ControllerContext, ActionDescriptor, object>> conditions)
{
this._conditions = conditions;
}
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
IEnumerable<Filter> result = from condition in _conditions
select condition(controllerContext, actionDescriptor)
into filter
where filter != null
select new Filter(filter, FilterScope.Global, null);
return result;
}
}
In globals.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
var conditions = new Func<ControllerContext, ActionDescriptor, object>[]
{
(c, a) =>
a.ControllerDescriptor.ControllerName.Equals("Online", StringComparison.OrdinalIgnoreCase)
? null : new RequireLocalHostActionFilter()
};
filters.Add(new ConditionalFilterProvider(conditions));
filters.Add(new HandleErrorAttribute());
}
I can see the action filter is added into the collection of filters. And last my OnlineController, a simpel control, that I want the global filter to kick in.
public class OnlineController : Controller
{
public Action开发者_运维问答Result Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC! online";
return View();
}
// thx for taking your time to read this post. // dennis
My if statement needed to be reversed.
var conditions = new Func < ControllerContext,
ActionDescriptor, object > [] {
(c, a) =>
a.ControllerDescriptor.ControllerName.Equals("Online",
StringComparison.OrdinalIgnoreCase) * * ? new RequireLocalHostActionFilterAttribute() : null * *
};
And I forgot to added the filter to the top of the controller.
// dennis
精彩评论