2013-04-23 47 views
2

在我的web應用程序中,我需要創建一個動作,使用http非安全連接可訪問哪些動作。只有https才能訪問其他所有內容。 我發現我可以使用[RequireHttps]屬性來控制器和操作。只允許一個動作在ASP.NET mvc 4上使用非安全連接,c#

但是有沒有像[AllowHttp]屬性?

我不想把所有控制器的動作[RequireHttps]屬性。將[RequireHttps]添加到控制器和[AllowHttp]可以更簡單地採取一種可以無安全訪問的操作。當然,如果這個屬性可用

我的意思是類似我們可以使用身份驗證。 [Authorize]給控制器,[AllowAnonymous]對於一些動作可以在沒有驗證的情況下訪問。

回答

3

正如你可以AuthorizeAttribute源看到它檢查AllowAnonymousAttribute存在

bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true) 
           || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true); 

所以,你可以從RequireHttps dervive自己的屬性,並添加相同的檢查,因此它看起來就像

if (!skipAuthorization && !filterContext.HttpContext.Request.IsSecureConnection) 
     { 

      HandleNonHttpsRequest(filterContext); 
     } 

並且AllowAnonymousAttribute的位置,您可以添加您自己的AllowHttpAttribute並用此新的AllowHttpAttribute標記您的操作

相關問題