Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/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自托管Web API上配置SSL_C#_Ssl_Asp.net Web Api - Fatal编程技术网

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

C# 在ASP.NET自托管Web API上配置SSL,c#,ssl,asp.net-web-api,C#,Ssl,Asp.net Web Api,我正在创建自托管Web API服务。为了保护它,我研究并实现了本文,使用makecert成功地生成了一个本地SSL证书,并且我的服务经过身份验证,如果我使用 http://localhost/webapi/authentication/authenticate 链接,但当我尝试使用HTTPS访问我的服务时,我在Firefox上看到以下内容: ssl\u错误\u接收\u记录\u过长 对于同样的请求,Fiddler告诉我: HTTP/1.1 502 Fiddler-连接失败日期:2013年8月26

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

http://localhost/webapi/authentication/authenticate
链接,但当我尝试使用HTTPS访问我的服务时,我在Firefox上看到以下内容:

ssl\u错误\u接收\u记录\u过长

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

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

[Fiddler]到本地主机的套接字连接失败
未能执行此操作 与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是:

我错过了什么? 提前谢谢

根据我的想法,我应该创建一个SSL证书并将其分配给特定的端口(:99)

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

netsh http add sslcert ipport=0.0.0:99 certhash=3e4906c01a774c888231e5092077d3d855a6861 appid={2d6059b2-cccb-4a83-ae08-8ce209c2c5c5c1}
,其中certhash=SSL指纹,appid=ApplicationId我之前复制过


就这样,现在我可以发出HTTPS请求了

使用代码的第一种方法:

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);
        }
    }
}
使用属性的第二种方法:如果不想使用第一种方法,可以使用
requireHttpAttribute
装饰控制器类或操作方法

  • 开放IIS
  • 双击“服务器证书”
  • 在右侧窗格中,单击“创建自签名证书”
  • 输入名称“localhost”
  • 完成
  • 将使用您指定的名称创建证书,请查找它

  • 双击证书,它将打开其属性,选择“指纹”并复制值
  • 现在您已经创建了证书,是时候将其绑定到您在自主机中使用的端口了。如果是

  • 开放式高架CMD
  • 运行此命令将您创建的证书绑定到您正在使用的端口
  • netsh-http-add-sslcert-ipport=0.0.0:5000 certhash=[cert-thumbprint]appid={[appid]}

  • 将[Cert thumbprint](包括方括号)替换为您在步骤6中复制的值
  • 替换[app id](包括方括号)是应用程序集信息中的guid
  • [程序集:Guid(“[app id]”)]


    你完了

    这里有一个很好的问题和一个有用的答案,但它的价值几乎完全取决于与外部资源的链接。您可能需要编辑您的答案,并添加一个特定于Web API的步骤列表,这些步骤是您为使其正常工作而采取的。这将提高您的答案对其他人的价值,以防超链接“腐烂”。您问题中的帖子链接看起来不正确:请更清楚地检查https:BaseAddress.Scheme==“https”我认为这应该被标记为答案。一步一步地解释一切。Than you@Mahmoudth这不应该被标记为答案,因为问题是关于自我托管,而不是使用IIS托管。但是,对于其他可能会在本页上寻找与IIS相关的解决方案的人来说,这是一个很好的答案。@karezza我想说明是说,仅使用IIS生成证书。现在如何安装IIS。。。
    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);
            }
        }
    }
    
    config.Filters.Add(new RequireHttpsAttribute());