如何在mvc6中启用https

Ras*_*kov 8 c# https asp.net-core-mvc

我是怎么做到这一点的:

1-启动时设置过滤器:

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

2-在cotroler中设置[RequireHttps]

[RequireHttps]
public class HomeController : BaseController
{
    public ViewResult Index()
    {
        return View();
    }
 }
Run Code Online (Sandbox Code Playgroud)

3-在project.json中添加

 "kestrel": "Microsoft.AspNet.Hosting --server=Microsoft.AspNet.Server.Kestrel --server.urls=https://localhost:1234"
Run Code Online (Sandbox Code Playgroud)

仍然没有工作.我做错了什么?

Max*_*ler 5

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

确保在Startup.cs/Configure方法中启用S​​SL .

它是这样完成的:

var certPath = "c:\\mycert.pfx";
app.UseKestrelHttps(new X509Certificate2(certPath, "certificatePassword"));
Run Code Online (Sandbox Code Playgroud)

动作过滤器将仅对实际URL起作用.您确实需要侦听具有证书的端口以获取HTTP.

希望这可以帮助.

来源样本Startup.cs