C# 来自AuthorizeAttribute AuthorizeCore的MVC4控制器类型和操作方法信息
我正在尝试创建一个授权系统,允许我使用控制器类型和操作方法信息中的各种数据来检查用户是否有权访问我系统的该部分C# 来自AuthorizeAttribute AuthorizeCore的MVC4控制器类型和操作方法信息,c#,asp.net-mvc-4,authorization,C#,Asp.net Mvc 4,Authorization,我正在尝试创建一个授权系统,允许我使用控制器类型和操作方法信息中的各种数据来检查用户是否有权访问我系统的该部分 public class NewAuth : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var Type = ControllerTypeHere; var MethodInfo = ActionMet
public class NewAuth : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var Type = ControllerTypeHere;
var MethodInfo = ActionMethodInfoHere;
}
}
是否有任何方法可以获取当前尝试访问的操作或控制器的类型和方法信息
如果需要,我可以提供任何其他信息。是获取请求相关信息的好方法
让我们对您的代码进行一些修改
public class NewAuth : AuthorizeAttribute
{
private Type _type;
private ActionDescriptor _actionDescriptor;
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
_type = filterContext.Controller.GetType();
_actionDescriptor = filterContext.ActionDescriptor;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// use _type
// use _actionDescriptor
return true;
}
}
OnAuthorization
首先触发,因此在调用AuthorizationCore
时将设置变量。您会注意到,我已将的概念更改为。它们是不可交换的,但ActionDescriptor是获取有关操作的一些常用信息的有用方法,例如ActionName
、FilterAttribute
、CustomAttributes
和参数
如果需要MethodInfo,它当然是可行的,但可能有点棘手。请记住,您的控制器可能有多个具有相同名称的操作;HttpGet版本、HttpPost版本等
在本例中,我正在寻找操作的[HttpPost]版本:
var methodInfo = _type.GetMethods()
.SingleOrDefault(mi => mi.Name == filterContext.ActionDescriptor.ActionName &&
mi.GetCustomAttributes(false).Any(attr => attr is HttpGetAttribute));
是获取请求相关信息的好方法
让我们对您的代码进行一些修改
public class NewAuth : AuthorizeAttribute
{
private Type _type;
private ActionDescriptor _actionDescriptor;
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
_type = filterContext.Controller.GetType();
_actionDescriptor = filterContext.ActionDescriptor;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// use _type
// use _actionDescriptor
return true;
}
}
OnAuthorization
首先触发,因此在调用AuthorizationCore
时将设置变量。您会注意到,我已将的概念更改为。它们是不可交换的,但ActionDescriptor是获取有关操作的一些常用信息的有用方法,例如ActionName
、FilterAttribute
、CustomAttributes
和参数
如果需要MethodInfo,它当然是可行的,但可能有点棘手。请记住,您的控制器可能有多个具有相同名称的操作;HttpGet版本、HttpPost版本等
在本例中,我正在寻找操作的[HttpPost]版本:
var methodInfo = _type.GetMethods()
.SingleOrDefault(mi => mi.Name == filterContext.ActionDescriptor.ActionName &&
mi.GetCustomAttributes(false).Any(attr => attr is HttpGetAttribute));
现在我不知道我的情况是否是由其他原因造成的,但是如果有人对此解决方案有问题,请尝试在调用
base.OnAuthorization(filterContext)之前分配私有字段代码>,我体验到,否则,AuthorizeCore
将抛出一个NullException,因为它们没有及时设置。无论如何,这个解决方案+1!是的,这实际上是倒退;base.OnAuthorization调用AuthorizeCore,因此在OnAuthorization中,您必须在调用base版本之前先进行设置现在我不知道我的情况是否是由其他原因造成的,但是如果有人对此解决方案有问题,请尝试在调用base.OnAuthorization(filterContext)之前分配私有字段代码>,我体验到,否则,AuthorizeCore
将抛出一个NullException,因为它们没有及时设置。无论如何,这个解决方案+1!是的,这实际上是倒退;base.OnAuthorization调用AuthorizeCore,因此在OnAuthorization中,您必须在调用基本版本之前先进行设置