ASP.NET Core Controller脚手架:证书问题

时间:2017-06-13 21:13:55

标签: asp.net-core ssl-certificate visual-studio-2017 asp.net-mvc-scaffolding asp.net-core-2.0

这与my other question

有关

大多数脚手架问题都已解决,但我收到与证书相关的错误:

enter image description here

在appsettings.json中我有以下内容:

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

并在appsettings.Development.json中:

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

环境就是发展:enter image description here

为什么要问生产证书?

为什么我需要脚手架证书?

2 个答案:

答案 0 :(得分:1)

Microsoft / EF here记录了此问题。

以下是其文档的直接复制和粘贴(上面链接)。

ASP.NET Core 2.0和Web Tools已知问题

尝试应用EF迁移或使用代码生成时出现证书错误

<强>问题:

尝试应用EF迁移或使用代码生成来构建ASP.NET Core 2.0应用程序中的代码时,会出现错误:&#39;没有名为&#39; HTTPS&#39;在当前环境(生产)的配置中找到。

解决方法:

启动开发人员命令提示符,设置环境变量ASPNETCORE_ENVIRONMENT = Development,然后使用此环境变量集启动VS

此外,我不认为您需要为脚手架设置SSL。如果您不需要SSL,我会禁用SSL并尝试链接文档建议的解决方法,无论当前显示在项目属性中的当前调试环境变量如何。希望它有所帮助。

答案 1 :(得分:1)

如果您仔细阅读文档,您会发现Kestrel不支持通过配置文件设置HTTPS端点。 Github asp.net/security中也存在一个问题,深入探讨了这个问题。您可以按照此模式在Program.cs文件中进行设置....

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