asp.net-mvc-5 - MVC5 身份验证 : Authorize attribute on every controller or base controller

标签 asp.net-mvc-5 asp.net-identity owin authorize-attribute

我一直在研究保护我的 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/

相关文章:

entity-framework - 无法创建 'ElectiveTesting.Models.ApplicationUser' 类型的常量值

c# - 如何在 asp.net vnext 中注册身份验证类型名称

ASP.NET MVC 5 属性路由 : Url. 操作返回 null

c# - 无法加载文件或程序集“Microsoft.ReportViewer.ProcessingObjectModel,版本=10.0.0.0”

c# - ASP.Net Core MVC Identity - 添加临时( session )声明

asp.net - 如何使 Identity.GetUserId() 返回 Guid 而不是字符串?

azure - 如何在 Azure Functions 中使用 OWIN?

asp.net-web-api - 如何在 Swashbuckle 中包含类和属性描述,使用 OWIN 为 Web Api 2 生成 Swagger 文档?

SignalR - OWIN - 多个端点

c# - 创建用户时出现 ObjectDisposedException