10

Is There one way to make a [Authorize] attibute be ignored in one action in a controller class that has a Authorize attribute?

        [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
           }
        }

Just the PrivateMethod() should have authentication required, but it has been required too.

PS: I wouldn't like to make my custom authorize filter.

[]'s

Adriano Zawadzki
  • 174
  • 1
  • 1
  • 8

5 Answers5

16

You can use [AllowAnonymous]

 [Authorize]
 public class MyController : Controller
 {
     [AllowAnonymous]
     public ActionResult PublicMethod()
     {
           //some code
     }

     public ActionResult PrivateMethod()
     {
           //some code
     }
  }
Erfan
  • 1,132
  • 15
  • 21
4

By default it's impossible - if you set [Authorize] for controller then only authenticated user can access to action.

or

You can try custom decisions: stackoverflow.

Community
  • 1
  • 1
vladimir
  • 13,428
  • 2
  • 44
  • 70
3

A solution is in this article: Securing your ASP.NET MVC 3 Application

The article talks about a white list approach where you decorate actions with a AllowAnonymous custom attribute. It requires that you extend AuthorizeAttribute and the OnAuthorization method to skip authorization checks of AllowAnonymous -actions. (The approach is credited to Levi, a security expert on the MVC team.)

Sola Oderinde
  • 1,046
  • 2
  • 22
  • 30
Omar
  • 1,493
  • 12
  • 14
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
       }
    }
  • Try adding some words to explain your answer. Answers of just code are normally less helpful. – Matt Mar 19 '17 at 00:19
0

Just for future reference This is now available to be done by the the [AllowAnonymous] attribute in ASP.NET MVC 4.

More Info

Salim
  • 439
  • 2
  • 8