获取授权属性调用的API方法名称

4

在我的自定义授权属性代码中,我希望能够识别调用了哪个WebAPI方法。

我知道可以通过传递名称来实现这一点(参见示例2),但我不想这样做。

// Example1
[CustomAuthAttribute]
public MyResponse get(string param1, string param2)
{
    ...
}
// in the prev example I would like to be able to identify the
// method from within the CustomAuthAttribute code


// Example2
[CustomAuthAttribute(MethodName = "mycontroller/get")]
public MyResponse get(string param1, string param2)
{
    ...
}
// in this example I pass the controller/method names to the
// CustomAuthAttribute code

有没有办法我能够把这个东西拿起来?

显示属性的定义 - Nkosi
我还没有得到一个 - 只是在计划中。 - CompanyDroneFromSector7G
1个回答

4

如果派生自AuthorizeAttribute,您可以通过HttpActionContext访问ActionDescriptor

public class CustomAuthAttribute : AuthorizeAttribute {    
    public override void OnAuthorization(HttpActionContext actionContext) {
        var actionDescriptor = actionContext.ActionDescriptor;
        var actionName = actionDescriptor.ActionName;
        var controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
        //MethodName = "mycontroller/get"
        var methodName = string.Format("{0}/{1}", controllerName, actionName);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接