Blaise
Blaise

Reputation: 22242

How to enable SSL in MVC 6 application?

In Visual Studio 2015, when we select the MVC6 web application, the Properties window contains no SSL Enabled property.

So what is the correct way to run a MVC6 application in SSL?


Since we can create pure Html + JavaScript site with the empty MVC 6 Application, can we enable SSL without using RequireHttpsAttribute that only comes with MVC?

Upvotes: 5

Views: 1559

Answers (2)

Dmitry S.
Dmitry S.

Reputation: 8513

There is nothing that needs to be done to enable SSL on the HTML/JavaScript side assuming your web server is set up with a certificate and proper bindings, and the firewall is configured properly.

If you are asking how to redirect to HTTPS automatically, it can be done using JavaScript. Put something like this in a JavaScript file that is referenced at the top of each page.

if (window.location.protocol.toLowerCase() != "https:") {
    window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
}

Upvotes: 0

Milen
Milen

Reputation: 8877

In your Startup.cs file options.Filters.Add(new RequireHttpsAttribute());

public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
    .......
    }


    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.Configure<MvcOptions>(options =>
        {
            .....
            options.Filters.Add(new RequireHttpsAttribute());
        });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                "default",
                "{controller)/{action}",
                 new { controller = "Home", action = "Index" }
                );
        });
    }
}

Upvotes: 1

Related Questions