问题
The AuthorizeAttribute shows up just fine, but for the life of me I can't figure out where the AllowAnonymousAttribute class is.
Whenever I add it to code, I get compiler errors.
[Authorize] //works fine
public ActionResult DoSomething(){
...
}
[AllowAnonymous] //COMPILER ERROR type not found. Red squigglies. Bad.
public ActionResult Foo() {
...
}
I'm in an MVC3 project.
回答1:
ASP.NET MVC 3, or more precisely the System.Web.Mvc version 3.0.0.0 assembly does not contain AllowAnonymousAttribute.
It was added in ASP.NET MVC 4: http://msdn.microsoft.com/en-us/library/system.web.mvc.allowanonymousattribute_methods%28v=vs.108%29.aspx
There's an AllowAnonymousAttribute in System.Web.Http assembly as well, but I could not get it working with my ASP.NET MVC 3 project: http://msdn.microsoft.com/en-us/library/system.web.http.allowanonymousattribute%28v=vs.108%29.aspx
回答2:
By default all actions provide access to anonymous users.
Here is the good example for security implementation with Anonymous attribute: http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx
Or this (example of custom [Authorize] attribute): http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/
来源:https://stackoverflow.com/questions/9363410/mvcs-allowanonymousattribute-not-showing-up