【问题标题】:Configuring SSL on ASP.NET Self-Hosted Web API在 ASP.NET 自承载 Web API 上配置 SSL
【发布时间】:2013-08-28 21:17:11
【问题描述】:

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

http://localhost/webapi/authentication/authenticate

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

ssl_error_rx_record_too_long

Fiddler 向我展示了相同的请求:

HTTP/1.1 502 Fiddler - 连接失败日期:2013 年 8 月 26 日星期一 10:44:27 GMT 内容类型:文本/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();

从this post 获取的 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);
    }
}

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

【问题讨论】:

标签: c# ssl asp.net-web-api


【解决方案1】:

根据this blog post 我发现,我应该创建一个 SSL 证书并将其分配给特定端口(在我的情况下为:99)。

我已经创建了本地签名的 SSL。然后得到了 Thumbprint 和 ApplicationId。使用 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 的步骤列表,您为使其正常工作而采取了这些步骤。如果超链接“腐烂”,这将提高您对其他人的回答的价值。
【解决方案2】:
  1. 打开 IIS
  2. 双击“服务器证书”
  3. 在右侧窗格中,单击“创建自签名证书”
  4. 输入名称“本地主机”
  5. 完成

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

  1. 双击证书,将打开其属性,选择“指纹”并复制值。

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

  1. 打开提升的 CMD
  2. 运行此命令将您创建的证书绑定到您正在使用的端口。

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

  1. 将 [Cert-thumbprint](包括方括号)替换为您在第 6 步中复制的值。
  2. 替换 [app-id](包括方括号)是来自应用程序集信息的 guid

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

你已经完成了!

【讨论】:

  • 我认为这应该被标记为答案。一步一步解释一切。谢谢@Mahmoud
  • 这不应被标记为答案,因为问题是关于自托管,而不是使用 IIS 托管。但是,对于可能最终在此页面上寻找与 IIS 相关的解决方案的其他人来说,这是一个很好的答案。
  • @karezza 我认为说明是说仅使用 IIS 来生成证书。现在如何安装 IIS...
【解决方案3】:

使用代码的第一种方法:

public class RequireHttpsAttribute : AuthorizationFilterAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
        {
            actionContext.Response = actionContext.Request
                .CreateResponse(HttpStatusCode.Found);
            actionContext.Response.Content = new StringContent
                ("<did>Use https instead of http</div>", Encoding.UTF8, "text/html");

            UriBuilder uriBuilder = new UriBuilder(actionContext.Request.RequestUri);
            uriBuilder.Scheme = Uri.UriSchemeHttps;
            uriBuilder.Port = 44337;

            actionContext.Response.Headers.Location = uriBuilder.Uri;
        }
        else
        {
            base.OnAuthorization(actionContext);
        }
    }
}

在 web 配置文件中输入以下代码:

config.Filters.Add(new RequireHttpsAttribute());

使用属性的第二种方法:如果不想使用第一种方法,可以用RequireHttpsAttribute装饰控制器类或动作方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 2015-02-25
    • 2019-03-11
    • 2012-08-04
    • 2012-03-11
    • 2018-07-23
    • 2012-10-13
    • 2016-10-25
    相关资源
    最近更新 更多