Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
Raskolnikov Avatar asked Nov 12 '15 13:11

Raskolnikov


People also ask

How do I enable HTTPS in Visual Studio?

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.


1 Answers

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

like image 50
Maxime Rouiller Avatar answered Sep 28 '22 02:09

Maxime Rouiller