My sample project is a MVC WebApi project.
The standard AuthorizeAttribute takes a Roles = "" or Users = "" parameter. I didn't have a look at the implementation yet, but I don't want to either :) Can you make this a little bit more dummy proof with expressions? In a way that you get a compile time error when you decide to rename the attribute's "Roles" property to anything else?
public interface IControllerContext
{
int MagicNumber { get; }
int ScaryNumber { get; }
string Version { get; }
}
public class AppAuthorizationAttribute : FilterAttribute
{
public IControllerContext SomeControllerContext
{
get; // implementation omitted
}
// ...
}
// This is my sample class which needs to be validated using the attribute.
public class TestClass : BaseClass
{
[AppAuthorization((n) => n.MagicNumber == 13)] // or literally: which property and how to validate it"
protected void SomeMethodWhichRequiresPreProcessingValidation()
{
// do something.
// inside here I have access to an instance of ISomeContext
// and can do anything I want.
}
}
Optional bonus question: Would it somehow be possible to access a field of the current controller (not static) from within the attribute definition? It would be a cool thing to inject a controller field into the validation lambda expression. Something like that: AppAuthorization((controller) => controller.SomePropertyHere.MagicNumer == 13)