SSL pages under ASP.NET MVC

前端 未结 12 1083
情话喂你
情话喂你 2020-11-28 01:49

How do I go about using HTTPS for some of the pages in my ASP.NET MVC based site?

Steve Sanderson has a pretty good tutorial on how to do this in a DRY way on Previe

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 02:33

    MVC 6 (ASP.NET Core 1.0) is working slightly different with Startup.cs.

    To use RequireHttpsAttribute (as mentioned in answer by Amadiere) on all pages, you could add this in Startup.cs instead of using attribute style on each controller (or instead of creating a BaseController for all your controllers to inherit from).

    Startup.cs - register filter:

    public void ConfigureServices(IServiceCollection services)
    {
        // TODO: Register other services
    
        services.AddMvc(options =>
        {
            options.Filters.Add(typeof(RequireHttpsAttribute));
        });
    }
    

    For more info about design decisions for above approach, see my answer on similar question about how to exclude localhost requests from being handled by the RequireHttpsAttribute.

提交回复
热议问题