I have a custom Attribute called AuthoriseAttribute whose constructor looks like this:
public AuthoriseAttribute(int userId)
{
.. blah
}
This is used with a method called GetUserDetails()
like this:
[Authorise(????????)]
public UserDetailsDto GetUserDetails(int userId)
{
.. blah
}
At runtime, the presence of the Authorise attribute causes some authorisation code to execute which requires the ID of the user. Obviously, this can be extracted from the parameter of the GetUserDetails()
method, but this means that the authorisation code depends on the method's parameter being given a particular name.
I would like to be able to pass in the actual value of the userId
parameter into the attribute, so that the authorisation code works with the value passed in to the attribute (i.e. not the method parameter), whose name is known.
Something like this (which doesn't work):
[Authorise(userId)]
public UserDetailsDto GetUserDetails(int userId)
{
.. blah
}
Is such a thing possible?
There is a way to do this _in ASP.NET MVC_ with action-methods (not with attributes in general)
public class CustomAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
int userId = (int)filterContext.ActionParameters["userId"];
}
}