我有一个用AuthorizeAttribute修饰的控制器.除了一个需要CustomAuthorizeAttribute提供的自定义身份验证的操作外,控制器还包含几个需要身份验证的操作.
我的问题是,一旦我在控制器级别添加[授权],我可以使用[CustomAuthorize]仅在一个操作上覆盖它(或删除它)吗?或者我是否必须从控制器级别删除[授权]并将其单独添加到其他每个操作?
我纯粹是为了方便,因为我很懒,不想用AuthorizeAttribute来装饰每一个动作.
[Authorize]
public class MyController : Controller {
//requires authentication
public ViewResult Admin() {
return View();
}
//... a lot more actions requiring authentication
//requires custom authentication
[CustomAuthorize] //never invoked as already failed at controller level
public ViewResult Home() {
return View();
}
}
Run Code Online (Sandbox Code Playgroud) 是否有一种方法可以在具有Authorize属性的控制器类中的一个操作中忽略[Authorize]属性?
[Authorize]
public class MyController : Controller
{
[Authorize(Users="?")]//I tried to do that and with "*", but unsuccessfuly,
public ActionResult PublicMethod()
{
//some code
}
public ActionResult PrivateMethod()
{
//some code
}
}
Run Code Online (Sandbox Code Playgroud)
只需要PrivateMethod()就可以进行身份验证,但也需要它.
PS:我不想制作我的自定义授权过滤器.
[]的