MVC's AllowAnonymousAttribute not showing up

萝らか妹 提交于 2019-12-11 00:56:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!