我一直在研究保护我的 MVC 5 应用程序的最佳方法。
我们有一个带有许多 WebAPI Controller 的 Web.csproj 和一个有两个区域的 MVC 站点 - 一个用于管理员,然后是面向公众的网站。
阅读后this article which states that the Base Controller is best way ,我决定采用这种方法。
但是,我个人不同意使用基本 Controller ( see this stackoverflow answer 用于我的一些推理)。
因此,鉴于我使用的是 MVC 5(ASP.Net 身份和 OWIN 身份验证) - 任何人都可以阐明每种方法的优缺点吗?
最佳答案
MVC 5 当前的做法是应用 AuthorizeAttribute
作为全局过滤器,并使用 AllowAnonymousAttribute
打开单个操作/ Controller
所以在 App_Start\FilterConfig.cs 添加以下几行:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
... existing filters
// use the [AllowAnonymous] attribute to open up individual Actions/Controllers
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
filters.Add(new RequireHttpsAttribute());
}
注:作为一个很好的措施,我还添加了 RequireHttpsAttribute,因为每个带有 ASP.Net Identity 的经过身份验证的请求都带有 auth cookie,如果通过常规 HTTP 进行,它很容易受到中间人攻击。
关于asp.net-mvc-5 - MVC5 身份验证 : Authorize attribute on every controller or base controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110518/