HTTP Windows服务到HTTPS

问题描述 投票:0回答:2

我有一个托管我的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
2个回答
0
投票

我们只需添加一个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

如果有任何我可以帮助的话,请随时与我联系。


-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.