How I tried to do this:
1- Set filter in startup:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//...
services.AddMvc();
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
2- set [RequireHttps] in cotroler
[RequireHttps]
public class HomeController : BaseController
{
public ViewResult Index()
{
return View();
}
}
3- add in project.json
"kestrel": "Microsoft.AspNet.Hosting --server=Microsoft.AspNet.Server.Kestrel --server.urls=https://localhost:1234"
And still not working. What have I done wrong?
You just select the project name in the solution and locate the property called “SSL Enabled” in properties window: The same properties window will also show the HTTPS URL for the application. In the above example, it's https://localhost:65440/. Copy that URL and go to the project properties window.
EDIT : This is a new feature that is not in beta8
yet. I've noticed after I've tried to find this feature in the beta8 tag on Github. It seems like your only solution for now is to either but it behind IIS (who supports HTTPS) or behind NGINX while will add that module for you.
Make sure to enable SSL in your Startup.cs/Configure
method.
It is done like so:
var certPath = "c:\\mycert.pfx";
app.UseKestrelHttps(new X509Certificate2(certPath, "certificatePassword"));
The action filters will just act on the actual URL. You do need to listen on a port with a certificate on it to have to HTTPs.
Hope this helps.
Source to sample Startup.cs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With