如何在mvc6中启用https

时间:2015-11-12 13:57:01

标签: c# https asp.net-core-mvc

我是如何尝试这样做的:

1-启动时设置过滤器:

     public IServiceProvider ConfigureServices(IServiceCollection services)
       {
        //...
        services.AddMvc();
        services.Configure<MvcOptions>(options =>
        {
            options.Filters.Add(new RequireHttpsAttribute());
        });

在cotroler中设置[RequireHttps]

[RequireHttps]
public class HomeController : BaseController
{
    public ViewResult Index()
    {
        return View();
    }
 }

3-添加到project.json

 "kestrel": "Microsoft.AspNet.Hosting --server=Microsoft.AspNet.Server.Kestrel --server.urls=https://localhost:1234"

仍然无法正常工作。 我做错了什么?

1 个答案:

答案 0 :(得分:5)

编辑:这是一项尚未在beta8中的新功能。在我尝试在Github上的beta8标签中找到此功能后,我注意到了。现在看来你唯一的解决办法就是在IIS(支持HTTPS)之后,或者在NGINX之后,同时为你添加该模块。

确保在Startup.cs/Configure方法中启用SSL。

它是这样完成的:

var certPath = "c:\\mycert.pfx";
app.UseKestrelHttps(new X509Certificate2(certPath, "certificatePassword"));

操作过滤器只会对实际网址执行操作。您确实需要监听带有证书的端口以获取HTTP。

希望这有帮助。

Source to sample Startup.cs