Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 403
Blaise Avatar asked Nov 09 '22 02:11

Blaise


1 Answers

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" }
                );
        });
    }
}
like image 131
Milen Avatar answered Jan 04 '23 01:01

Milen