在ASP.NET自托管Web API上配置SSL

ins*_*um_ 36 c# ssl asp.net-web-api

我正在创建自托管Web API服务.为了保护它,我已经研究并实现了这篇文章,使用makecert成功生成了本地SSL证书并且我的服务已经过身份验证,如果我正在使用,则会生成令牌.

http://localhost/webapi/authentication/authenticate
Run Code Online (Sandbox Code Playgroud)

链接,但是当我尝试使用HTTPS访问我的服务时,我会在Firefox上关注:

SSL_ERROR_RX_RECORD_TOO_LONG

而对于同样的要求,Fiddler告诉我:

HTTP/1.1 502 Fiddler - 连接失败日期:2013年8月26日星期一10:44:27 GMT内容类型:text/html; charset = UTF-8连接:关闭时间戳:13:44:27.433

[Fiddler]与localhost的套接字连接失败.
无法与server.fiddler.network.https协商HTTPS连接>无法保护localhost的现有连接.由于意外的数据包格式,握手失败.

我的自主配置:

    private HttpSelfHostServer _server;
    private ExtendedHttpsSelfHostConfiguration _config;
    public const string ServiceAddress = "https://localhost/webapi";
    _config = new ExtendedHttpsSelfHostConfiguration(ServiceAddress);
    _server = new HttpSelfHostServer(_config);
    _server.OpenAsync();
Run Code Online (Sandbox Code Playgroud)

这篇文章中获取的ExtendedHttpSelfHostConfiguration 是:

public class ExtendedHttpSelfHostConfiguration : HttpSelfHostConfiguration
{
    public ExtendedHttpSelfHostConfiguration(string baseAddress) : base(baseAddress) { }
    public ExtendedHttpSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { }

    protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
    {
        if (BaseAddress.ToString().ToLower().Contains("https://"))
        {
            httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
        }

        return base.OnConfigureBinding(httpBinding);
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?提前致谢!

ins*_*um_ 35

根据我发现的这篇博文,我应该创建一个SSL证书并将其分配给特定端口(在我的情况下为99).

我已经创建了本地签名的SSL.然后得到它的ThumbprintApplicationId.使用CMD命令netsh(在Win7之前的系统中有一个httpcfg工具),我已经将我的证书分配给了端口

netsh http add sslcert ipport=0.0.0.0:99 certhash=3e49906c01a774c888231e5092077d3d855a6861 appid={2d6059b2-cccb-4a83-ae08-8ce209c2c5c1},其中certhash = SSL Thumbprint,appid = ApplicationId我之前已经复制过.

就是这样,现在我能够发出HTTPS请求了!

  • 你在这里有一个很好的问题,也是一个有用的答案,但它的价值几乎完全取决于外部资源的链接.您可能希望编辑答案,并添加特定于Web API的步骤列表,以使其工作.如果超链接"腐烂",这将增强您对其他人的回答的价值. (32认同)

小智 5

  1. 打开IIS
  2. 双击“服务器证书”
  3. 在右侧窗格中,单击“创建自签名证书”
  4. 输入名称“ localhost”

将使用您指定的名称创建证书,找到它。

  1. 双击证书,它将打开其属性,选择“缩略图”并复制值。

现在您已经创建了证书,是时候将其绑定到您在自助主机中使用的端口上了。如果是https:// localhost:5000

  1. 开放式高架CMD
  2. 运行此命令将您创建的证书绑定到您正在使用的端口。

netsh http add sslcert ipport=0.0.0.0:5000 certhash=[cert-thumbprint] appid={[App-Id]}

  1. 用您在步骤6中复制的值替换[证书缩略图](包括方括号)。
  2. 将[app-id](包括方括号)替换为来自应用程序组装信息的向导

[assembly: Guid("[app-id]")]

大功告成!

  • 这不应被标记为答案,因为问题是关于自托管,而不是使用 IIS 托管。然而,对于其他可能最终在此页面上寻找与 IIS 相关的解决方案的人来说,这是一个很好的答案。 (2认同)