9

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:

  1. Disable SSL in Development;
  2. 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?

4 Answers 4

17

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)

Configure SSL in SSL

Sign up to request clarification or add additional context in comments.

1 Comment

This is the right answer. IIS Express makes it easy to use a self-signed cert to test locally. You may have to set the SslPort property on MvcOptions in order to make the redirect work properly in dev, FYI.
11

If you don't want to use IIS Express then delete the https-address in Project Properties -> Debug section -> Under "Web Server Settings" -> Uncheck "Enable SSL".

ProjectProperties

2 Comments

Welcome to stackoverflow. Please ensure you answer the question and that you fully explain your answer.
Dear mr @Simon.S.A., this is how you can disable ssl if you want to debug without using iis.
5

just comment this line:

rewriteOptions.AddRedirectToHttps();

or in new versions of .Net core on Startup.cs comment:

app.UseHttpsRedirection();

Comments

-3

Using #if !DEBUG, like below:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.