是的,这可以使用所有控制器继承的基本控制器类,也可以通过创建一个自定义属性来装饰控制器。
基本控制器:
public class BaseController : Controller
{
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
// verify logic here
}
}
您的控制器:
public class AccountController : BaseController
{
// the Initialize() function will get called for every request
// thus running the verify logic
}
自定义授权属性:
public class AuthorizeAccountNumberAttribute : AuthorizationAttribute
{
protected override AuthorizationResult IsAuthorized(System.Security.Principal.IPrincipal principal, AuthorizationContext authorizationContext)
{
// verify logic here
}
}
在控制器上:
[AuthorizeAccountNumber]
public class AccountController : Controller
{
// the IsAuthorized() function in the AuthorizeAccountNumber will
// get called for every request and thus the verify logic
}
您可以将这两种方法结合起来,使用
[AuthorizeAccountNumber]
需要验证的控制器从中继承。