【问题标题】:Streaming MTOM Attachments in WCF and Certificate Authentication在 WCF 和证书身份验证中流式传输 MTOM 附件
【发布时间】:2011-10-29 00:25:18
【问题描述】:

我正在寻找关于我正在尝试做的事情是否受支持的明确答案。

基本上,我使用 WCF 流式传输大型 MTOM 附件 (200 Mb),这非常有效。该服务的安全要求是使用 HTTPS 和基于证书的身份验证。我可以通过 HTTPS 运行服务而没有任何问题,但是一旦我将 IIS 设置为“接受客户端证书”或“需要客户端证书”(代码中没有更改),就会引发以下错误(但只有在附件结束后80 Mb 左右):

The socket connection was aborted. 
This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. 
Local socket timeout was '00:30:00'.

我找到了一些资源,很抱歉现在找不到它们,这表明失败可能与传入消息无法数字签名或由于消息内容的流式性质而无法验证有关。我认为该服务必须对整个消息内容进行哈希处理以验证证书,但这无法实现,因为在尝试进行验证时,部分消息正在传输中。

我已经设置了消息契约,使得正文是单个 Stream 元素,而其他元素包含在标头中:

   <MessageContract()>
Public Class StreamAttachmentRequest

    <MessageHeader(MustUnderstand:=True)>
    Public Property AttachmentName As String

    <MessageBodyMember(Order:=1)>
    Public Property Attachment As Stream

End Class

服务配置如下:

<system.serviceModel>

<!-- BINDING -->
<bindings>
<basicHttpBinding>
  <binding name="TestCaseBasicBinding"
           messageEncoding="Mtom"
           transferMode="StreamedRequest"
           maxReceivedMessageSize="2147483647"
           closeTimeout="00:30:00"
           openTimeout="00:30:00"
           receiveTimeout="00:30:00"
           sendTimeout="00:30:00">
    <security mode="Transport">
      <transport clientCredentialType="None"></transport>
    </security>
    <readerQuotas maxDepth="32"
                      maxStringContentLength="8192"
                      maxArrayLength="16384"
                      maxBytesPerRead="4096"
                      maxNameTableCharCount="16384" />
  </binding>
</basicHttpBinding>
</bindings>

<!-- BEHAVIORS -->
<behaviors>
  <serviceBehaviors>

    <!-- TEST CASE SECURE BEHAVIOR -->
    <behavior name="TestCaseSecureBehavior">
      <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceCredentials>
        <serviceCertificate
            storeLocation="LocalMachine"
            storeName="My"
            findValue="DistinguishedNameOfCert"
            x509FindType="FindBySubjectDistinguishedName" />
        <clientCertificate>
          <authentication certificateValidationMode="ChainTrust"/>
        </clientCertificate>
      </serviceCredentials>
    </behavior>

  </serviceBehaviors>
</behaviors>

<!-- SERVICES -->
<services>
  <service name="StreamingMutualAuthTestCase.Web.Service.TestCaseServiceImplementation" 
           behaviorConfiguration="TestCaseSecureBehavior">
    <!-- SERVICE -->
    <endpoint address=""
              binding="basicHttpBinding"
              bindingConfiguration="TestCaseBasicBinding"
              contract="StreamingMutualAuthTestCase.Web.Service.ITestCaseService" />

    <endpoint contract="IMetadataExchange" binding="mexHttpsBinding" address="mex" />

  </service>
</services>    

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

客户端配置如下:

 <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ITestCaseService" closeTimeout="00:30:00"
                openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                maxReceivedMessageSize="2147483647" messageEncoding="Mtom"
                transferMode="Streamed">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="Certificate" realm="" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>

  <!-- BEHAVIORS -->
  <behaviors>
    <endpointBehaviors>
      <behavior name="SecureClientBehavior">
        <clientCredentials>
          <clientCertificate
            storeLocation="LocalMachine"
            storeName="My"
            findValue="DistinguishedNameOfCert"
            x509FindType="FindBySubjectDistinguishedName"/>
          <serviceCertificate>
            <authentication certificateValidationMode="ChainTrust"/>
          </serviceCertificate>
        </clientCredentials>
      </behavior>
    </endpointBehaviors>
  </behaviors>

    <client>
        <endpoint address="https://test7/TestCaseService/TestCaseService.svc" 
                  binding="basicHttpBinding" 
                  bindingConfiguration="BasicHttpBinding_ITestCaseService"
                  contract="TestCaseService.ITestCaseService" 
                  name="BasicHttpBinding_ITestCaseService" 
                  behaviorConfiguration="SecureClientBehavior"/>
    </client>
</system.serviceModel>

再一次,在我将 IIS 客户端证书设置为接受或要求之前,这将正常工作。

另外,IIS 日志中有 413 错误...

2011-08-18 15:00:06 W3SVC1 10.39.8.111 POST /TestCaseService/TestCaseService.svc - 443 - 10.75.13.81 - - - test7 413 0 0

我已经在我的文件上传服务之上设计了一个身份验证服务来解决这些问题;但我真的很想知道我正在尝试做的事情是否“可行”。

非常感谢 - 帕特里克

【问题讨论】:

  • 你有没有查过 HTTP 代码 413 是什么意思?请求太大 - checkupdown.com/status/E413.html
  • 有完整的源代码示例工作的最终解决方案吗?
  • @Kiquenet 我最终重构了我在问题中提到的解决方案。我写了一个特定的身份验证令牌服务,它返回一个过期令牌,以便在后续请求中传递。

标签: wcf streaming certificate x509 mtom


【解决方案1】:

如果你想在 IIS 中打开客户端证书,你必须为你的服务(和客户端)做同样的事情:

<security mode="Transport">
  <transport clientCredentialType="Certificate" />
</security>

您的客户必须向代理提供证书:

yourProxy.ClientCredentials.ClientCertificate.SetCertificate(...);

此外,您的服务器必须信任这些证书,因此客户端证书必须由服务器信任的证书颁发机构颁发,或者它们必须直接安装到服务器上的 LocalMachine\Trusted people store。

WCF 端点不支持“接受客户端证书” - 您必须使用客户端证书或不使用。

【讨论】:

  • 感谢 Ladislav 提供的信息,我已经能够让服务与证书身份验证一起工作,所以我知道这部分工作正常。将客户端凭据类型设置为 None 或 Certificate 都没有关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 2011-04-09
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多