Raskolnikov
Raskolnikov

Reputation: 3999

How to enable https in mvc6

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?

Upvotes: 8

Views: 1282

Answers (1)

Maxime Rouiller
Maxime Rouiller

Reputation: 13699

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

Upvotes: 5

Related Questions