【问题标题】:How to selfhost .net core 3.1 web api如何自托管 .net core 3.1 web api
【发布时间】:2020-06-22 12:42:08
【问题描述】:

任何人都可以举例说明如何在没有 IIS 的情况下自托管核心 Web API。我已经在 IIS 中托管,但我想执行自托管,并且我想为 selfhost Web API 启用 HTTPS

【问题讨论】:

  • 您使用 VS(VS 代码或命令)创建的 ASP.NET Core Web API 项目是自托管的。代码既可以在 IIS 上运行,也可以在 IIS 外部运行。

标签: asp.net-core-webapi


【解决方案1】:
  1. 自托管web api请参考official documentation
  2. 启用 https

在提升的控制台(“以管理员身份运行”)上,执行 netsh http add urlacl url=https://+:4443/ user=<your user name> 允许正在运行的用户使用 HTTPS 监听端口 4443(注意在上面的命令中使用 https 而不是 http)。

同样在提升的控制台上,通过运行注册服务器证书

netsh http add sslcert ipport=0.0.0.0:port certhash=thumbprint appid={
    app-guid
}

在哪里, port 是监听端口(例如 4443);特殊 IP 地址 0.0.0.0 匹配本地机器的任何 IP 地址; thumbprint 是证书的 SHA-1 哈希,以十六进制表示; app-guid 是任何 GUID(例如 {00000000-0000-0000-0000-000000000000}),用于标识拥有的应用程序。 编写自己的主机配置,如

class MyHttpsSelfHostConfiguration : HttpSelfHostConfiguration
{
    public MyHttpsSelfHostConfiguration(string baseAddress): base(baseAddress){}
    public MyHttpsSelfHostConfiguration(Uri baseAddress) : base(baseAddress){}
    protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
    {
        httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
        return base.OnConfigureBinding(httpBinding);
    }
}

然后更改传递给 MyHttpsSelfHostConfiguration 构造函数的基地址:var config = new MyHttpsSelfHostConfiguration(“https://localhost:4443”);

【讨论】:

  • 我认为这适用于 .net 框架,我也可以将其用于 .net core web APi 吗?
猜你喜欢
  • 2020-09-15
  • 2017-02-05
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多