繁体   English   中英

如何在MVC6中启用https

[英]How to enable https in mvc6

我如何尝试做到这一点:

1-在启动时设置过滤器:

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

2-在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"

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

编辑:这是beta8尚未提供的新功能。 在Github的beta8标签中尝试找到此功能后,我注意到了。 看来您目前唯一的解决方案是将其添加到IIS(支持HTTPS)或NGINX后面,同时为您添加该模块。

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

它是这样完成的:

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

动作过滤器将仅作用于实际的URL。 您确实需要侦听带有证书的端口,才能使用HTTP。

希望这可以帮助。

样本Startup.cs的来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM