Asp.net mvc 在ASP.net MVC中从授权中排除某些操作

Asp.net mvc 在ASP.net MVC中从授权中排除某些操作,asp.net-mvc,authentication,Asp.net Mvc,Authentication,在我的控件顶部有一个authorize属性,这意味着它包含我的所有操作。 我想从该属性中排除一些操作(匿名用户可以使用这些操作)。可能吗 [Authorize] public class TestController : Controller { public ActionResult Index() { ... } ... //available by anonymous public ActionResult Test() {

在我的控件顶部有一个authorize属性,这意味着它包含我的所有操作。 我想从该属性中排除一些操作(匿名用户可以使用这些操作)。可能吗

[Authorize]
public class TestController : Controller
{
   public ActionResult Index()
   {
     ...
   }
   ...

   //available by anonymous
   public ActionResult Test()
   {
     ...
   }
}

您可能希望将该属性放在受限制的操作之上,而将其他操作(您希望允许匿名访问的操作)放在一边


另外,将其从类的顶部删除。

[Authorize]
属性放在控制器上基本上是将其放在每个操作上的快捷方式,因此您的代码在逻辑上等同于

// No [Authorize] here
public class TestController : Controller
{
    [Authorize]
    public ActionResult Index()
    {
         // code here...
    }

    [Authorize]
    public ActionResult Test()
    {
         // code here...
    }
}
您可能可以看到我的意图-从控制器中删除该属性,并将其放在要限制的特定操作上:

// No [Authorize] here
public class TestController : Controller
{
    [Authorize]
    public ActionResult Index()
    {
         // code here...
    }

    // no [Authorize] here either, so anonymous users can access it...
    public ActionResult Test()
    {
         // code here...
    }
}

您可以采用本博客文章中概述的方法,创建一个
AllowAnonymous
属性,并将该属性放置在您希望排除的操作上:


从MVC4开始,
AllowAnonymous
属性是库存的,可以根据需要应用。

我的博客的更新版本可以在