Okay, I’m obviously missing something, as this should be pretty simple. I’ve created the following attribute:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class EntitleAttribute : Attribute
{
public EntitleAttribute(string permissionName)
{
bool hasPermission = ...
if (!hasPermission)
{
throw new HttpException(403, "Forbidden");
}
}
}
I put it on a method:
[Entitle("Access Application")]
public ActionResult MyMethod(...) { ... }
However, it never gets called. What am I missing?
Attributes don’t do anything by themselves. There needs to be some code that would look for the attribute and do something with it.
Based on code sample you want to perform some per-authentication in ASP.Net MVC application. In this case you should derive your attribute from MVC’s AuthorizeAttribute. If you need more general handling in MVC site – base FilterAttribute or even IMvcFilter provide more options.