将AuthorizeAttribute应用于控制器类并同时采取行动
问题描述:
是否有一种方法可以在具有Authorize特性的控制器类中的一个操作中忽略[Authorize] attibute?将AuthorizeAttribute应用于控制器类并同时采取行动
[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
}
}
只是PrivateMethod()应该有认证要求,但它也被要求。
PS:我不想让我的自定义授权过滤器。
的
答
一种解决方案是在这篇文章中:Securing your ASP.NET MVC 3 Application
在哪里你装饰用使用AllowAnonymous自定义属性操作的白名单方法的文章会谈。它要求您扩展AuthorizeAttribute
和OnAuthorization
方法以跳过授权检查AllowAnonymous -actions。 (该方法是记李维斯,在MVC团队的安全专家。)
答
您可以用[使用AllowAnonymous]
[Authorize]
public class MyController : Controller
{
[AllowAnonymous]
public ActionResult PublicMethod()
{
//some code
}
public ActionResult PrivateMethod()
{
//some code
}
}
答
public class MyController : Controller
{
[Authorize] //it will only work for the following action
public ActionResult PublicMethod()
{
//some code
}
public ActionResult PrivateMethod() //[Authorize] will not work for this action
{
//some code
}
}
+0
尝试添加一些单词来解释您的答案。正义代码的答案通常不太有用。 – Matt
你们是不是要无视授权即对类myController的对行动PublicMethod或PrivateMethod? – itsmatt
这是忽略行动PublicMethod。我写错了,我很抱歉,现在已经修好了! –