ASP.NET MVC 2 - How to override an annotation for a single controller action?
I have a BaseController that all Controllers inherit from. The CaseController has the [Authorize] annotation, so that all controllers require authorization.
But I've just realized开发者_Python百科 that one single controller action must not require authorization. How can I turn off [Authorize] for just that one controller action?
using System.Web.Mvc;
using Unleashed.Service.Interfaces;
namespace Controllers
{
[Authorize]
[RequireHttps]
public class BaseController : Controller
{
}
}
And the controller action is called via a POST from another site. They authorize by passing a token. They will not be authorized via forms authentication.
[Authorize=false] // doesnt compile
[Authorize(false)] // doesnt compile
public ActionResult DoSomething(string token, string data)
{
}
Solved by creating a new controller that does not inherit from BaseController, and adding the action to that controller. Now this one action does not require authorization, and all existing actions remain as is.
精彩评论