在ASP.NET Self-Hosted Web API上配置SSL

本文关键字:配置 SSL API Web ASP NET Self-Hosted | 更新日期: 2023-09-27 18:29:05

我正在创建Self-hosted Web API服务。为了确保它的安全,我研究并实现了这篇文章,使用makecert成功地生成了本地SSL证书,并且我的服务经过了身份验证,如果我使用,则可以很好地生成令牌

http://localhost/webapi/authentication/authenticate

链接,但当我尝试使用HTTPS访问我的服务时,我会在Firefox上得到以下内容:

ssl_error_rx_record_to_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连接;未能保护本地主机的现有连接。握手由于意外的数据包格式而失败。。

我的自主机配置:

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

其中,本文中的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);
    }
}

我缺少什么?提前感谢!

在ASP.NET Self-Hosted Web API上配置SSL

根据我的这篇博客文章,我应该创建一个SSL证书并将其分配给特定的端口(在我的情况下是:99)。

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

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

就是这样,现在我可以进行HTTPS请求了!

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

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

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

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

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

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

  1. 将[Cert指纹](包括方括号)替换为在步骤6中复制的值
  2. 替换[app id](包括方括号)是应用程序集信息中的guid

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

你完了!

使用代码的第一种方法:

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

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