Enable / Disable SSL in Core ASP.NET Projects in Development

In an ASP.NET Core project, I am using SSL in Production, so I have in Startup:

public void ConfigureServices(IServiceCollection services) {
  services.AddMvc(x => {
    x.Filters.Add(new RequireHttpsAttribute());
  });  
  // Remaining code ...
}

public void Configure(IApplicationBuilder builder, IHostingEnvironment environment, ILoggerFactory logger, IApplicationLifetime lifetime) {
  RewriteOptions rewriteOptions = new RewriteOptions();
  rewriteOptions.AddRedirectToHttps();
  builder.UseRewriter(rewriteOptions);
  // Remaining code ...
}

      

It works great in manufacturing, but not in development. I would also like:

  • Disable SSL in development;
  • Make SSL work in development, because with the current configuration it doesn't. Do I need to install any PFX files on my local machine?
    Am I working on multiple projects to create problems?
+3


source to share


2 answers


You can configure the service using the interface IConfigureOptions<T>

.

internal class ConfigureMvcOptions : IConfigureOptions<MvcOptions>
{
    private readonly IHostingEnvironment _env;
    public ConfigureMvcOptions(IHostingEnvironment env)
    {
        _env = env;
    }

    public void Configure(MvcOptions options)
    {
        if (_env.IsDevelopment())
        {
            options.SslPort = 44523;
        }
        else
        {
            options.Filters.Add(new RequireHttpsAttribute());
        }
    }
}

      

Then add this class as a singleton:



public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    var builder = services.AddMvc();
    services.AddSingleton<IConfigureOptions<MvcOptions>, ConfigureMvcOptions>();
}

      

Regarding the SSL point, you can easily use SSL using IIS Express ( source )

Setting up SSL to SSL

+7


source


Using #if !DEBUG

as below:



public void ConfigureServices(IServiceCollection services) {
  services.AddMvc(x => {
    #if !DEBUG
    x.Filters.Add(new RequireHttpsAttribute());
    #endif
  });  
  // Remaining code ...
}

      

-1


source







All Articles