Enable / Disable SSL on ASP.NET Core projects in Development

asp.net-core

Solution

You can configure a service using the `IConfigureOptions<T>` interface.

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>();
}

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

Problem

On 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 fine in Production but not in Development. I would like to either: - Disable SSL in Development; - Make SSL work in Development because with current configuration it is not. Do I need to set any PFX files on my local machine? I am working on multiple projects so that might create problems?

Original source