2017-06-18 108 views
1

我在我的IIS中安裝了自己的https證書,並在我的本地計算機中將我的EchoService從http更改爲https。HTTPS合同不匹配問題WCF

但是我選擇運輸安保後出現錯誤。

由於EndpointDispatcher中的ContractFilter不匹配,無法在接收方處理帶Action的消息。這可能是因爲合同不匹配(發件人和收件人之間的操作不匹配)或發件人和收件人之間的綁定/安全性不匹配。檢查發送方和接收方是否有相同的合同和相同的綁定(包括安全要求,例如消息,傳輸,無)。

客戶端應用程序配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" /> 
    </startup> 
    <system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="EchoService"> 
      <security mode="Transport"> 
      <transport clientCredentialType="None" proxyCredentialType="None" realm=""></transport> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://desktop-rfd4j23/WCFASPNETSecurityEx/EchoService.svc" binding="wsHttpBinding" 
       bindingConfiguration="EchoService" contract="EchoService.IEchoService" name="EchoService"> 
     </endpoint> 
    </client> 
    </system.serviceModel> 
</configuration> 

服務Web配置:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.6.2"/> 
    <httpRuntime targetFramework="4.6.2"/> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/> 
    </httpModules> 
    </system.web> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" 
     type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" 
     type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/> 
    </compilers> 
    </system.codedom> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false"/> 
    <modules> 
     <remove name="ApplicationInsightsWebTracking"/> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" 
     preCondition="managedHandler"/> 
    </modules> 
    </system.webServer> 
    <system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
     <binding name="wsHttp" hostNameComparisonMode="Exact"> 
      <security mode="Transport" /> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WCFASPNETSecurityEx.EchoServiceBehavior"> 
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="WCFASPNETSecurityEx.EchoServiceBehavior" name="WCFASPNETSecurityEx.EchoService"> 
     <endpoint binding="webHttpBinding" bindingConfiguration="wsHttp" 
        name="EchoService" contract="WCFASPNETSecurityEx.IEchoService"> 
     </endpoint> 
     </service> 
    </services> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
</configuration> 

客戶端代碼:

Console.WriteLine("Press Enter when the web server is started"); 
Console.ReadLine(); 

EchoServiceClient client = new EchoServiceClient(); 
String result = client.Echo("Hello WCF"); 
Console.WriteLine(result); 
Console.ReadLine(); 

我剛開g調用client.Echo(「Hello WCF」)時出錯。請幫我解決這個問題。謝謝。

+0

您確定您使用https訪問且證書未頒發給本地主機。 –

回答

0

我應該提到wsHttpBinding而不是webHttpBinding。

<bindings> 
     <wsHttpBinding> 
     <binding name="wsHttp" hostNameComparisonMode="Exact"> 
      <security mode="Transport" > 
      <transport clientCredentialType="None" proxyCredentialType="None" realm=""></transport> 
      <message clientCredentialType="UserName"></message> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
0

您與您的https運輸鎖定下來,需要用戶名認證。 不應在安全模式設置爲:

<security mode="TransportWithMessageCredential" > 

你處理的是在客戶端的任何地方?

<serviceCredentials> 
    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="xxx.MyUserNameValidator,xxx.WCF.Security"/> 
</serviceCredentials>