0

This is related to my other question.

Most of scaffolding problem were resolved, but I'm getting error related to certificate:

enter image description here

In appsettings.json I have the following:

"Kestrel": {
    "Endpoints": {
      "Localhost": {
        "Address": "127.0.0.1",
        "Port": "53688"
      },
      "LocalhostHttps": {
        "Address": "127.0.0.1",
        "Port": "44384",
        "Certificate": "HTTPS"
      }
    }
  },

and in appsettings.Development.json:

"Certificates": {
    "HTTPS": {
      "Source": "Store",
      "StoreLocation": "LocalMachine",
      "StoreName": "My",
      "Subject": "CN=localhost",
      "AllowInvalid": true
    },

And Environment is Development:enter image description here

Why it's asking Certificate for Production?

And why I need Certificate for scaffolding?

Sanket
  • 19,295
  • 10
  • 71
  • 82
Alexan
  • 8,165
  • 14
  • 74
  • 101

2 Answers2

1

This issue is documented by Microsoft/EF here.

The following is a direct copy and paste of their documentation (linked above).

ASP.NET Core 2.0 and Web Tools Known Issues

Certificate error when trying to apply EF migrations or using code generation

Issue:

When trying to apply EF migrations or when using code generation to scaffold code in an ASP.NET Core 2.0 application, you get error: 'No certificate named 'HTTPS' found in configuration for the current environment (Production).

Workaround:

Start a Developer Command Prompt, set the environment variable ASPNETCORE_ENVIRONMENT=Development and then start VS with this environment variable set

Also, I would not assume that you need to setup SSL for the purpose of scaffolding. I would disable SSL if you do not require it and try the workaround suggested by the linked documentation regardless of the current debug environment variables currently displayed in the project properties. Hopefully it helps.

  • now I get the following error: "Cannot resolve scoped service '...Context' from root provider" – Alexan Jun 21 '17 at 13:33
  • @Alex : Check out this related Stack Overflow thread [here](https://stackoverflow.com/questions/44180773/dependency-injection-in-asp-net-core-2-thows-exception?rq=1). – William Wright Jun 21 '17 at 14:36
  • Actually I think there are good reasons to always run using SSL even during development. It isn't hard to set up so why not? – GlennSills Sep 02 '17 at 16:15
1

If you dig around in the documentation a bit you'll see Kestrel does not support setting up an HTTPS endpoint via a configuration file. There is also a issue in Github asp.net/security that covers this in depth. You can set things up in the Program.cs file following this pattern....

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        IHostingEnvironment env = null;

        return WebHost.CreateDefaultBuilder(args)
              .UseStartup<Startup>()
              .ConfigureAppConfiguration((hostingContext, config) =>
              {
                  env = hostingContext.HostingEnvironment;

                  config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

                  if (env.IsDevelopment())
                  {
                      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                      if (appAssembly != null)
                      {
                          config.AddUserSecrets(appAssembly, optional: true);
                      }
                  }

                  config.AddEnvironmentVariables();

                  if (args != null)
                  {
                      config.AddCommandLine(args);
                  }
              })
              .UseKestrel(options =>
              {
                  if (env.IsDevelopment())
                  {
                      options.Listen(IPAddress.Loopback, 44321, listenOptions =>
                      {
                          listenOptions.UseHttps("testcert.pfx", "ordinary");
                      });
                  }
                  else
                  {
                      options.Listen(IPAddress.Loopback, 5000);
                  }
              })
              .Build();
    }
}
GlennSills
  • 3,977
  • 26
  • 28