HTTPS上的自托管WEB API。拒绝所有HTTP调用

HTTPS上的自托管WEB API。拒绝所有HTTP调用,api,web,https,host,self,Api,Web,Https,Host,Self,我有一个自我托管的WEB API项目,只需要在HTTPS上运行 我找到了几篇关于如何实现这一点的文章(获取SSL证书,使用'netsh'命令将证书绑定到IP/端口,将基址更改为HTTPS://…,等等) 到目前为止一切都很好,我可以通过HTTPS调用该服务 但我还需要明确拒绝所有HTTP调用。当我尝试使用HTTP调用该服务时,我收到“504超时”错误。并且需要一段时间才能从该服务获得响应,可能会收到拒绝服务 我尝试应用一个自定义属性,该属性将检查请求是否通过HTTPS发送,但从未到达该代码 为了

我有一个自我托管的WEB API项目,只需要在HTTPS上运行

我找到了几篇关于如何实现这一点的文章(获取SSL证书,使用'netsh'命令将证书绑定到IP/端口,将基址更改为HTTPS://…,等等)

到目前为止一切都很好,我可以通过HTTPS调用该服务

但我还需要明确拒绝所有HTTP调用。当我尝试使用HTTP调用该服务时,我收到“504超时”错误。并且需要一段时间才能从该服务获得响应,可能会收到拒绝服务

我尝试应用一个自定义属性,该属性将检查请求是否通过HTTPS发送,但从未到达该代码

为了拒绝HTTP调用,我需要做什么呢?同样,这是一个自宿主WEB API项目

我没有在IIS中托管WEB API。它是一个自托管WEB API。以下是我如何初始化它的代码:

public class HttpsSelfHostConfiguration : HttpSelfHostConfiguration
{
   public HttpsSelfHostConfiguration(string baseAddress) : base(baseAddress) { }
   public HttpsSelfHostConfiguration(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);
  }
}

var url = "https://localhost:7080/";

var config = new HttpsSelfHostConfiguration(url);

config.Routes.MapHttpRoute("WebAPIRoute2", "api/{controller}/{action}/{id}",
      new { controller = "Submissions", id = RouteParameter.Optional });


var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
我还使用以下命令将SSL证书绑定到端口7080:

netsh http add urlacl url=https://+:7080/ user=Everyone

netsh http add sslcert ipport=0.0.0.0:7080 certhash=Thumbprint appid={xxxx}

我猜你需要这样的东西


受保护的无效应用程序\u BeginRequest(对象发送方,事件参数e)
{
if(HttpContext.Current.Request.IsSecureConnection.Equals(false)和&HttpContext.Current.Request.IsLocal.Equals(false))
{
Response.Redirect(“https://”+Request.ServerVariables[“HTTP_HOST”]
+HttpContext.Current.Request.RawUrl);
}
}

您需要告诉我们有关您的设置的更多信息。代码前面的Web服务器?哪个Web服务器?端口80上是否有侦听器?API使用哪种语言?…我也遇到了同样的问题。您找到了解决方案吗?不,我没有。我所做的只是将端口显式绑定到HTTPS协议。所有HTTP请求超时。我没有其他解决方案:(已经有一段时间了。我也在为此寻找解决方案。到现在为止,大约3年后,你找到什么了吗?)??
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
                        redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security"
                        pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000" />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>


protected void Application_BeginRequest(Object sender, EventArgs e)
{
   if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
   {
    Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
+   HttpContext.Current.Request.RawUrl);
   }
}