How to enable SSL in MVC 6 application?

In Visual Studio 2015, when you select the MVC6 web application, the Properties window does not contain the SSL Enabled property.

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


Since we can create a clean Html + JavaScript site using the empty MVC 6 application, can we enable SSL without using the RequireHttpsAttribute , which only comes with MVC?

+5
source share
2 answers

In the file Startup.cs 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" } ); }); } } 
+1
source

There is nothing that needs to be done to enable SSL on the HTML / JavaScript side, provided that your web server is configured with a certificate and associated bindings and the firewall is configured correctly.

If you ask how to automatically redirect HTTPS, this can be done using JavaScript. Put something like this in the JavaScript file 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); } 
0
source

Source: https://habr.com/ru/post/1234776/


All Articles