是否有一种方法可以在具有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
}
}
只有PrivateMethod()应该需要身份验证,但也需要它。
PS:我不想制作自定义授权过滤器。
[]的
答案 0 :(得分:16)
您可以使用[AllowAnonymous]
[Authorize]
public class MyController : Controller
{
[AllowAnonymous]
public ActionResult PublicMethod()
{
//some code
}
public ActionResult PrivateMethod()
{
//some code
}
}
答案 1 :(得分:4)
答案 2 :(得分:3)
本文提供了一个解决方案:Securing your ASP.NET MVC 3 Application
本文讨论了一种白名单方法,您可以使用 AllowAnonymous 自定义属性来装饰操作。它要求您扩展AuthorizeAttribute
和OnAuthorization
方法以跳过 AllowAnonymous -actions的授权检查。 (这种方法归功于Lev,MVC团队的安全专家。)
答案 3 :(得分:0)
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
}
}
答案 4 :(得分:0)
仅供参考,现在可以通过ASP.NET MVC 4中的select a.social_status, count(*)
from client a
JOIN client b
ON a.id = b.id
where 1 = 1
and b.id = 1
and a.social_status = b.social_status
and a.created_at between '2018-09-10 06:05:41'::timestamp - interval '14 day' and '2018-09-10 06:05:41'::timestamp
group by a.social_status
属性来完成此操作。