wcf - 如何使用 MutualSSL 设置将服务引用配置为指向 Https?

标签 wcf ssl https

在我的项目中,我最近更改了我的 WCF 服务以使用 Https。它被配置为相互 ssl 设置,并且客户端和服务器证书都已正确安装。服务器端看起来很好,甚至在浏览器中启动也很好,如下所示。 WCF_Https

但是,当尝试从 WPF 客户端(之前添加和生成的服务代理)配置服务引用时。我收到 403 forbidden 错误代码,如下所示。知道为什么吗?

enter image description here

这是我的配置。

WCF 服务器端配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceCredentials>
            <serviceCertificate storeLocation="LocalMachine" x509FindType="FindByIssuerName" findValue="QuickFire Root Authority" />
          </serviceCredentials>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="PushNotification_SignalR_PoC.WCF.PushNotificationService">
        <endpoint binding="wsHttpBinding" bindingConfiguration="MutualSslLargeMessageBinding" contract="PushNotification_SignalR_PoC.WCF.IPushNotificationService" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="MutualSslLargeMessageBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Transport">
            <transport clientCredentialType="Certificate"></transport>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

WPF 客户端配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WsHttpBinding_IPushNotificationService"
                 closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:30:00" sendTimeout="00:01:00"
                 allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                 messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <client>
      <endpoint address="https://localhost:44367/PushNotificationService.svc"
          binding="wsHttpBinding" bindingConfiguration="WsHttpBinding_IPushNotificationService"
          contract="ServiceProxy.IPushNotificationService" name="WsHttpBinding_IPushNotificationService" />
    </client>

    <behaviors>
      <endpointBehaviors>
        <behavior name="MutualSslBehavior">
          <clientCredentials>
            <clientCertificate storeLocation="CurrentUser" x509FindType="FindBySubjectName" findValue="QuickFire Test Client"/>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

最佳答案

如果我们想通过添加服务引用来调用服务,我们应该在服务器端的服务端点中添加MEX端点。它可以在所有平台上交换服务的元数据。 如下所示,

<services>
      <service name="PushNotification_SignalR_PoC.WCF.PushNotificationService">
        <endpoint binding="wsHttpBinding" bindingConfiguration="MutualSslLargeMessageBinding" contract="PushNotification_SignalR_PoC.WCF.IPushNotificationService" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"></endpoint>
      </service>
</services>

详情,
https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-configure-a-custom-ws-metadata-exchange-binding
如果有什么我可以帮忙的,请随时告诉我。

关于wcf - 如何使用 MutualSSL 设置将服务引用配置为指向 Https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944727/

相关文章:

node.js - 从nodejs中的证书获取公钥

amazon-web-services - 亚马逊 S3 - HTTPS/SSL - 这可能吗?

linux - 将 HTTP 重定向到 HTTPS。最好的方法是什么?

c# - 序列化异常 : "Type\...\in assembly\...\is not marked as serializable" appears when I try to serialize ObservableCollection

c# - 如何访问有关 WCF 服务部署位置的信息

c# - WCF 服务方法中单例模式的问题

使用 Curl 将文件发布到 Web 服务的 SSL 握手错误

wcf - 为什么我不能在我的 WCF 类中使用 KnownType 属性?

python - 如何在 Ubuntu Trusty 上安全地使用 pip(带 SSL)?

apache - 两个 VirtualHosts 通过 https(运行 Redmine 等)