【问题标题】:HTTP Windows Service to HTTPSHTTP Windows 服务到 HTTPS
【发布时间】:2019-08-12 12:44:18
【问题描述】:

我有一个托管我的 wcf 服务的 Windows 服务。

app.config 是:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="RestWCFServiceLibrary.Service1Behavior" name="RestWCFServiceLibrary.RestWCFServiceLibrary">
        <endpoint address="" binding="webHttpBinding" contract="RestWCFServiceLibrary.IRestWCFServiceLibrary" behaviorConfiguration="web">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8888/RestWCFServiceLibrary/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestWCFServiceLibrary.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        <CorsSupport/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  <extensions>
            <behaviorExtensions>
                <add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            </behaviorExtensions>
        </extensions>
  </system.serviceModel>

</configuration>

我的问题是,如果我的网站使用 https://,由于 CORS,它无法进行 http 调用。 https 网站向 localhost 发出 ajax GET 请求。

现在我正在尝试将我的 Windows 服务更改为 https,但我到处都能看到一些命令行 ssl 绑定。有没有其他方法可以将我的 wcf 自托管 Windows 服务更改为使用 https?

我需要做什么才能将此 http 服务迁移到 https。

请在我的 app.config 中提供需要修改的示例。

【问题讨论】:

    标签: c# windows wcf service


    【解决方案1】:

    我偶然发现了类似的问题,所以我使用 WCF 配置工具为我编写了一个 App.Config,并且在端点内部我选择了 mexhttpsbinding 和 yaa https 绑定有效..

    【讨论】:

      【解决方案2】:

      让我们添加一个 https 端点。以下配置在 http 和 https 上都能正常工作。

        <system.serviceModel>
          <services>
            <service behaviorConfiguration="mybehavior" name="WcfService1.Service1">
              <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webbev"></endpoint>
              <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webbev" bindingConfiguration="myhttpsbinding"></endpoint>
              <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"></endpoint>
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:11010"/>
                  <add baseAddress="https://localhost:11011"/>
                </baseAddresses>
              </host>
            </service>
          </services>
          <bindings>
            <webHttpBinding>
              <binding name="myhttpsbinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" sendTimeout="00:10:00" receiveTimeout="00:10:00">
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
                <security mode="Transport">
                  <transport clientCredentialType="None"></transport>
                </security>
              </binding>
            </webHttpBinding>
          </bindings>
          <behaviors>
            <serviceBehaviors>
              <behavior name="mybehavior">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
              </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
              <behavior name="webbev">
                <webHttp />
              </behavior>
            </endpointBehaviors>
      </behaviors>
      

      由于 https 协议受证书保护,我们应该将证书绑定到 https 端点的 https 端口。 (如果在 IIS 中托管服务,我们可以在 IIS 绑定模块中指定证书而不是 CMD)

      netsh http add sslcert ipport=0.0.0.0:11011 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
      

      以管理员权限执行 CMD 并确保证书已安装在本地计算机证书存储 (certlm.msc) 上。 Certhash 参数指定证书的指纹。 appid 参数是一个 GUID,可用于识别所属应用程序(位于 project.csproj 文件中)

      <ProjectGuid>{56FDE5B9-3821-49DB-82D3-9DCE376D950A}</ProjectGuid>
      

      https://docs.microsoft.com/en-us/windows/desktop/http/add-sslcert
      https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate

      如果有什么我可以帮助的,请随时与我联系。

      【讨论】:

      • 啊,这看起来像是一个可能对我有用的解决方案,我不是通过 IIS 托管它是一个自托管的 Windows 服务。你能给我发消息吗?我没有直接联系你的方法。
      • 我可以和你谈谈你的解决方案吗?
      • 抱歉,稍后回复。我正在休假,请告诉我你的问题。
      【解决方案3】:

      Https 仅适用于端口 443。因此,您最好在服务器配置的 SSL 脚本标签中创建虚拟主机。 或者您也可以代理将您的请求 http:// 端口(8888)传递给 https://(端口:443)

      【讨论】:

      • 您能否详细说明如何在 wcf dll 的 c# windows 自托管服务中进行代理传递?
      • SSL 与 443 端口无关。我们有运行 domain.com:6579 的 https 站点
      • 这就是我的想法,我已经看到将它绑定到不同端口的命令行命令。现在我要弄清楚如何为我的服务做这件事,避免不得不手动打开 cmd 并以编程方式完成
      • 只有 443 个??你确定
      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 2017-05-21
      • 2022-09-23
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多