Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my ASP.Net MVC4 WEBAPI ActionFilter being ignored at runtime?

I'm trying to use an ActionFilter attribute so I can intercept the OnActionExecuting event and do a token validation in my web api code... Below is some code stripped of my security things(a pretty boilerplate code that checks if the token is in a database with Entity Framework):

public class TokenValidationFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var data = new ActionData();
            if (filterContext.HttpContext.Request.QueryString.AllKeys.Contains("token"))
            {
                using (var context = new GNCCustomers()) 
                {
                    data.GNCToken = context.Tokens.Where(t => t.GNCToken == filterContext.HttpContext.Request["token"]).SingleOrDefault();
                    if (data.GNCToken == null)
                    {
                        filterContext.Result = new HttpUnauthorizedResult("MissingOrInvalidToken") ;
                        return;
                    }
                }
                data.HttpVerb = filterContext.HttpContext.Request.HttpMethod;

                foreach (string item in filterContext.HttpContext.Request.QueryString)
                {
                    data.Params.Add(item, filterContext.HttpContext.Request.QueryString[item]);
                }
                data.Path = filterContext.RouteData.Values;

                filterContext.ActionParameters["actionData"] = data;
            }


            base.OnActionExecuting(filterContext);
        }

    }

I just applied it to one action method at my controller like this:

public class CustomerController : ApiController
    {
    [HttpPost]        
            [TokenValidationFilterAttribute]
            public HttpResponseMessage Create(ActionData actionData)
            {

                return Request.CreateResponse(HttpStatusCode.OK);
            }...

So, the problem is, if I put a breakpoint in the filter code, at the very first line, we didn't even get the breakpoint hit. I've also add some Debug.Print() stubs to check if is there any problem on my environment... Nothing... The code just don't run.

So, anyone has any clue on how to handle this??? I'm getting insane here checking for an solution and seems that this actionFilters are not so well documented. There is only one page in MSDN and saying nothing more that I've already implemented...

Thanks! I really appreciate any help.

Regards...

like image 384
Gutemberg Ribeiro Avatar asked Dec 18 '12 14:12

Gutemberg Ribeiro


1 Answers

Most probably you have derived from System.Web.Mvc.ActionFilterAttribute not from System.Web.Http.Filters.ActionFilterAttribute.

The first one is from ASP.NET MVC and the second one is from ASP.NET Web API - you want the second one as you are delivering from ApiController (so you are most probably hosting ASP.NET Web API inside ASP.NET MVC application).

like image 54
tpeczek Avatar answered Sep 28 '22 00:09

tpeczek