How to plug method parameters into custom attribute

前端 未结 4 1090
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 08:13

I have a custom Attribute called AuthoriseAttribute whose constructor looks like this:

public AuthoriseAttribute(int userId)
{
  .. blah
}

This

相关标签:
4条回答
  • 2021-02-19 08:58

    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"];
        }
    }
    
    0 讨论(0)
  • 2021-02-19 09:01

    Probably because this is an old post but this is now possible

    [MyAttribute(MyAttributeVar= "myMethodVar")]
    public void MyMethod(int myMethodVar)
    
    0 讨论(0)
  • 2021-02-19 09:08

    I was able to get around this by using the following:

    public class AuthorizeAttribute
    {
        protected bool RequireIdClaim { get; private set; }
    
        public AuthorizeAttribute(bool requireIdClaim = false)
        {
            RequireIdClaim = requireIdClaim;
        }
    
        public Authorize() 
        {
            //regular auth stuff here
    
            if (RequireIdClaim)
            {
                var routeData = context.ActionContext.Request.GetRouteData();
                var requiredIdClaim = Convert.ToInt32(routeData.Values["id"]); 
    
                //Check here if their user profile has a claim to that Id
            }
        }
    }
    

    And then on the specific methods you want to check Ids on,

    [HttpGet]
    [Route("{id}")]
    [Authorize(requireIdClaim: true)]
    public UserDetailsDto GetUserDetails(int userId)
    {
        .. blah
    }
    

    And if you don't care to check their Id, but just that they're authenticated

    [HttpGet]
    [Route("")]
    [Authorize]
    public bool isLoggedIn()
    {
        .. blah
    }
    

    Of course you can organize your authorize procedure however you like but this idea allows you to get their ID in your auth procedure there since it is passed in as route data. More here: https://stackoverflow.com/a/16054886

    0 讨论(0)
  • 2021-02-19 09:09

    Making vcsjones' comment an answer, this is not possible.

    Attributes are metadata; they are compiled into the assembly at compile-time and do not change during runtime. As such, any parameters you pass into an attribute must be constants; literals, constant variables, compiler defines, etc.

    The one way this would work is to make the attribute an AOP element, using a framework like PostSharp or rolling your own with the Unity Framework etc. This would allow you to attach an "interceptor" to the method by decorating it with an attribute, which will then run code in the attribute and will also have knowledge about exactly how the method was called including parameter values. Check out this blog: http://www.progware.org/Blog/post/Interception-and-Interceptors-in-C-(Aspect-oriented-programming).aspx

    0 讨论(0)
提交回复
热议问题