gpt4 book ai didi

c# - 如何在mvc6中启用https

转载 作者:太空狗 更新时间:2023-10-29 19:40:49 28 4
gpt4 key购买 nike

我是如何尝试做到这一点的:

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

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

2- 在 Controller 中设置 [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"));

Action 过滤器只会作用于实际的 URL。您确实需要监听带有证书的端 Eloquent 能使用 HTTP。

希望这对您有所帮助。

Source to sample Startup.cs

关于c# - 如何在mvc6中启用https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com