Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Core 3.1 Web API:是否可以作为Windows服务使用https自托管,并使用IIS之类的证书?_C#_Asp.net Core_Asp.net Core Webapi - Fatal编程技术网

C# ASP.NET Core 3.1 Web API:是否可以作为Windows服务使用https自托管,并使用IIS之类的证书?

C# ASP.NET Core 3.1 Web API:是否可以作为Windows服务使用https自托管,并使用IIS之类的证书?,c#,asp.net-core,asp.net-core-webapi,C#,Asp.net Core,Asp.net Core Webapi,我正在使用默认的ASP.NET Core 3.1 Web API应用程序,我已将其配置为https,并使用app.UseHttpsRedirection()也是 现在,我使用以下nuget软件包将此作为Windows服务托管:Microsoft.Extensions.hosting.WindowsServices 托管已完成,但我正在使用http获得API结果,但在尝试使用httpslikehttps://localhost:5001/weatherforecast 我可以创建一些像IIS这样的

我正在使用默认的ASP.NET Core 3.1 Web API应用程序,我已将其配置为
https
,并使用
app.UseHttpsRedirection()也是

现在,我使用以下nuget软件包将此作为Windows服务托管:
Microsoft.Extensions.hosting.WindowsServices

托管已完成,但我正在使用http获得API结果,但在尝试使用
https
like
https://localhost:5001/weatherforecast


我可以创建一些像IIS这样的自签名证书并分配它,并且可以作为https运行吗?

是的,您可以-但是有一些浏览器限制

创建证书,然后在证书存储中注册,或手动将其加载到Kestrel中,如:

certificate.json

{
  "certificateSettings": {
    "fileName": "localhost.pfx",
    "password": "YourSecurePassword"
  }
}
{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "Https": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "<path to .pfx file>",
          "Password": "<certificate password>"
        }
      }
    }
  }
}
并使用类似这样的方式:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddEnvironmentVariables()
            .AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"certificate.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
            .Build();

        var certificateSettings = config.GetSection("certificateSettings");
        string certificateFileName = certificateSettings.GetValue<string>("filename");
        string certificatePassword = certificateSettings.GetValue<string>("password");

        var certificate = new X509Certificate2(certificateFileName, certificatePassword);

        var host = new WebHostBuilder()
            .UseKestrel(
                options =>
                {
                    options.AddServerHeader = false;
                    options.Listen(IPAddress.Loopback, 443, listenOptions =>
                    {
                        listenOptions.UseHttps(certificate);
                    });
                }
            )
            .UseConfiguration(config)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .UseUrls("https://localhost:443")
            .Build();

        host.Run();
    }
}
公共类程序
{
公共静态void Main(字符串[]args)
{
var config=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddenEnvironmentVariables()
.AddJsonFile(“certificate.json”,可选:true,重载更改:true)
.AddJsonFile($“certificate.{Environment.GetEnvironmentVariable(“ASPNETCORE_Environment”)}.json”,可选:true,重载更改:true)
.Build();
var certificateSettings=config.GetSection(“certificateSettings”);
字符串certificateFileName=certificateSettings.GetValue(“文件名”);
字符串certificatePassword=certificateSettings.GetValue(“密码”);
var证书=新的X509Certificate2(certificateFileName,certificatePassword);
var host=new WebHostBuilder()
UseKestrel先生(
选项=>
{
options.AddServerHeader=false;
options.Listen(IPAddress.Loopback,443,listenOptions=>
{
UseHttps(证书);
});
}
)
.UseConfiguration(配置)
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup()
.useURL(“https://localhost:443")
.Build();
host.Run();
}
}

摘自这篇博文的片段:

@Frank Nielsen的答案是正确的,但如果其他人想要不同的方法,我想再添加一个方法

创建证书的部分与@Franck Nielsen在博客上发布的链接相同。就在这种方法中,所有配置都是通过
appsettings.json
自动完成的

ASP.NET Core 5.0文档说明:

CreateDefaultBuilder
默认情况下调用
Configure(context.Configuration.GetSection(“Kestrel”)
以加载Kestrel配置

因此,您应该将“Kestrel”部分添加到
appsetting.json

{
  "certificateSettings": {
    "fileName": "localhost.pfx",
    "password": "YourSecurePassword"
  }
}
{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "Https": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "<path to .pfx file>",
          "Password": "<certificate password>"
        }
      }
    }
  }
}
维奥拉,剩下的都是框架式的

我发现这种方法更干净,而且在windows服务中获取应用程序
.exe
的根目录时也不费力


链接到ASP.NET Core 5.0文档:

以及
https
request-stacktrace?本地主机拒绝连接的错误是什么。尝试:检查连接检查代理和防火墙但是您正在收听
443
(ssl)?尝试运行
netstat-aon-ptcp | findstr侦听
,查看是否存在
443
。好的,谢谢。我们将对此进行调查。gr8t,谢谢,非常感谢