Authorization attribute not working in MVC

I had been forced to use MVC, and needed to add an authorization filter to a controller action. No worries:

[Authorization]
public ActionResult View(int itemId) {
 …
}

And my actual filter code:

    public class AuthorizationAttribute: AuthorizationFilterAttribute {
        public override void OnAuthorization(HttpActionContext actionContext) {
            …
        }
    }

But, my breakpoint inside the OnAuthorization method wasn’t firing.

Turns out that I was using the wrong AuthorizationFilterAttribute! I needed to use the one in the System.Web.Mvc namespace, not the one lurking in System.Web.Http. So my corrected code looked like so:

    public class AuthorizationAttribute : System.Web.Mvc.AuthorizeAttribute {
        protected override bool AuthorizeCore(HttpContextBase httpContext) {
            …
            return true;
        }
    }
Advertisements
  1. Running and eating, two of my favorite things | Wunderhund

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: