authorizeattribute 继承自定义参数
authorizeattribute 继承自定义参数
在ASP.NET MVC中,AuthorizeAttribute
类是用来进行授权检查的。如果你想要在AuthorizeAttribute
中添加自定义参数,你可以通过以下几种方式来实现:
-
通过构造函数传递参数
-
使用属性
-
使用方法重写
以下是一个如何通过构造函数和属性传递参数的例子:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public string CustomParameter { get; set; }
public CustomAuthorizeAttribute(string customParameter)
{
this.CustomParameter = customParameter;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// 使用CustomParameter进行授权检查
// ...
return true; // 或者 false 如果未授权
}
}
然后,你可以在控制器或者动作方法上使用这个自定义属性:
[
public ActionResult SecureAction()
{
// 这个动作方法现在只对提供了特定参数的用户开放
// ...
}
记住,AuthorizeAttribute
类本身已经有一些可以被重写的方法,如OnAuthorization
,AuthorizeCore
等,你可以在这些方法中使用你的自定义参数。
漫思