簡體   English   中英

WCF證書自定義驗證 - 向客戶端發送異常消息

[英]WCF Certificate custom validation - send exception message to client

我有一個簡單的WCF服務配置為具有傳輸安全性和證書客戶端憑據。 我將自定義證書驗證程序插入其中,並拋出FaultException。 我的問題是esception消息沒有到達客戶端:我只收到MessageSecurityException而沒有任何故障...如果我在net.tcp(自托管)中轉換我的服務,我接收一個CommunicationExcetion,沒有任何錯誤。

這是我的web.config的一部分:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpEndpointBinding" messageEncoding="Text">
          <reliableSession enabled="false" />
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="SecureService.Server.ChunkStreamService">
        <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" behaviorConfiguration="myBehavior" name="wsHttpEndPoint" contract="SecureService.Server.IChunkStreamService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <clientCertificate>
              <authentication  customCertificateValidatorType="SecureService.Server.CPSValidator, SecureService.Server" certificateValidationMode="Custom" />
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

和我的驗證員:

    public class CPSValidator : System.IdentityModel.Selectors.X509CertificateValidator
  {
    public override void Validate(System.Security.Cryptography.X509Certificates.X509Certificate2 certificate)
    {
       throw new FaultException("Certificate is not from a trusted issuer");
    }
  }

任何想法 ?

在此先感謝您的幫助!

據我所知,您在驗證器中拋出的任何異常都不會到達客戶端。 客戶端始終收到MessageSecurityException。 只有在服務級別(在服務實現中)發生並映射到FaultContract的異常才會正確發送到客戶端。

關心巴勃羅。

正如Yahia和Pablo所回答的那樣,證書驗證不會在服務級別進行。 因此,FaultException無法到達客戶端。 但這是真的,因為我正在使用傳輸安全模式。 為了將消息發送回客戶端,我必須將我的安全模式從Transport更改為TransportWithMessageCredentials,如下所示:

<security mode="TransportWithMessageCredential">
  <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
  <message clientCredentialType="Certificate" negotiateServiceCredential="true" algorithmSuite="Default" />
</security>

這樣,證書驗證在服務級別完成,因此可以通過faultException將錯誤消息發送到客戶端。

您是否嘗試在服務器上啟用WCF診斷以在呼叫到達服務之前捕獲WCF管道中的問題?

<system.diagnostics>
<sources>
  <source name="System.ServiceModel" switchValue="Verbose" propagateActivity="true">
    <listeners>
      <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="log.evtlog"  />
    </listeners>
  </source>
</sources>
<trace autoflush="true"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM