2012-02-20 51 views
2

AuthorizeAttribute顯示出來很好,但對於我的生活,我無法弄清楚AllowAnonymousAttribute類的位置。MVC的AllowAnonymousAttribute沒有出現

每當我將它添加到代碼,我得到編譯器錯誤。

[Authorize] //works fine 
public ActionResult DoSomething(){ 
    ... 
} 

[AllowAnonymous] //COMPILER ERROR type not found. Red squigglies. Bad. 
public ActionResult Foo() { 
    ... 
} 

我在一個MVC3項目。

+0

AllowAnonymous - 這是一個自定義屬性?... – 2012-02-20 15:19:51

+1

@EvgeniyLabunskiy不,它是一個MVC4屬性。 – 2013-05-07 12:04:00

+0

@Husein我很驚訝,導致標題的主題是asp.net-mvc-3 – 2013-05-07 12:27:57

回答

5

ASP.NET MVC 3,或者更精確地說System.Web.Mvc版本3.0.0.0程序集不包含AllowAnonymousAttribute。

它在ASP.NET MVC 4補充說:http://msdn.microsoft.com/en-us/library/system.web.mvc.allowanonymousattribute_methods%28v=vs.108%29.aspx

有一個AllowAnonymousAttribute在System.Web.Http組裝爲好,但我無法得到它與我的ASP.NET MVC 3項目的工作:http://msdn.microsoft.com/en-us/library/system.web.http.allowanonymousattribute%28v=vs.108%29.aspx

0

默認情況下,所有操作都提供對匿名用戶的訪問。

這裏是匿名屬性的安全實現很好的例子: http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx

還是這個(自定義的例子[授權]屬性): http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/

+0

是的,當然。我需要做的更多的是所有行動都需要認證的情況,而只有少數行動可以允許匿名訪問。 – 2012-02-20 18:51:35

+0

第一個例子完全代表你需要:) – 2012-02-20 20:02:50