我如何尝试做到这一点:
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的IIS)或NGINX后面。
确保在您的Startup.cs/Configure
方法中启用SSL 。
它是这样完成的:
var certPath = "c:\\mycert.pfx";
app.UseKestrelHttps(new X509Certificate2(certPath, "certificatePassword"));
动作过滤器将仅作用于实际的URL。您确实需要侦听带有证书的端口,才能使用HTTP。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句