views:

220

answers:

2

I got a ASP.NET MVC controller like this

[Authorize]
public class ObjectController : Controller
{
     public ObjectController(IDataService dataService)
     {
         DataService = dataService;
     }
     public IDataService DataService { get;set;}
}

The Authorize attribute is defined as "Inherited=true" in the framework. So when i make the next controller:

public class DemoObjectController : ObjectController 
{
    public DemoObjectController(IDataService dataService)
        : base (dataService)
    {
        DataService = new DemoDataService(DataService);
    }
}

It gets the authorize attribute, but i don't want it here. I want the Demo Object controller to be available to everyone, cause it just uses fake data.

I guess I'll implement my own Authorize attribute that don't get inherited, for i can't find any way to remove the attribute from the inherited class.

A: 

If a base type requires authorization then all child types ought to require authorization as well. I think that you ought to inherit from a different type but a workaround would be to declare your own AthorizeAttribute that works for this particular instance.

Andrew Hare
+1  A: 

Since it is marked as inherited, there isn't much you can do in this case (since you don't control the code that is checking for the attribute via reflection). Implementing your own attribute seems the most practical option.

With MVC you can also often achieve the same functionality with overrides (the On* methods), which might be worth looking into.

Marc Gravell