【问题标题】:WCF 2-way ssl not workingWCF 2-way ssl 不工作
【发布时间】:2014-09-12 17:24:33
【问题描述】:

我是 WCF 的新手。我为客户端创建了一个自托管的 WCF 服务器,它是一个 java rest 客户端。客户端和服务器之间的通信应该通过两端的 ssl 证书相互验证。因此,在通信过程中,客户端需要发送证书。客户端证书需要在服务器上进行自定义验证。 我认为单向通信进行得很好,但服务器无法验证客户端证书。实际上,自定义验证器代码并没有自行执行。

在服务器跟踪中,我看到两次“未找到配置评估上下文”,猜测配置文件有问题

我的配置文件如下:

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="All, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\log\Traces.svclog" />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="mybinding">
          <transactionFlow />
          <textMessageEncoding />
          <httpsTransport requireClientCertificate="true" />
          <security authenticationMode="MutualSslNegotiated"/>
        </binding>
      </customBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviour">
          <serviceMetadata httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="Custom" customCertificateValidatorType="myproject.MyX509CertificateValidator,myproject"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="myHost" behaviorConfiguration="behaviour">
        <endpoint address="" contract="IIWCFServer" binding="customBinding" bindingConfiguration="mybinding" />
        <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpsBinding"/>
      </service>
    </services>
    <diagnostics>
      <messageLogging logEntireMessage="true"
                      logMessagesAtServiceLevel="true"
                      logMessagesAtTransportLevel="true"
                      logMalformedMessages="true"
                      maxMessagesToLog="5000"
                      maxSizeOfMessageToLog="2000">
      </messageLogging>
    </diagnostics>
  </system.serviceModel>
</configuration>

我已经阅读了 100 篇文章,但无法找到解决方案。任何建议都会有所帮助。

XML 异常的详细信息如下。如果我可以从任何其他地方获取错误详细信息,请告诉我。

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>524312</EventID>
<Type>3</Type>
<SubType Name="Warning">0</SubType>
<Level>4</Level>
<TimeCreated SystemTime="2014-04-21T09:09:53.2168282Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{28fb55cc-1d5f-4a5a-a76e-5939a733b8f1}" />
<Execution ProcessName="testServer.vshost" ProcessID="2368" ThreadID="9" />
<Channel />
<Computer>WGP-PRINT-145</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Warning">
<TraceIdentifier>http://msdn.microsoft.com/en-IN/library/System.ServiceModel.EvaluationContextNotFound.aspx</TraceIdentifier>
<Description>Configuration evaluation context not found.</Description>
<AppDomain>testServer.vshost.exe</AppDomain>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>

【问题讨论】:

  • 能否提供异常的详细信息?参考Configuration evaluation context not found。您是否尝试在配置中提供端点地址(因为 mex 端点需要主机基地址)?使用 authenticationMode="MutualSslNegotiated" 而不是 authenticationMode="MutualCertificate" 的任何原因?
  • 感谢回复,其实我不是很清楚authenticationMode="MutualCertificate/MutualCertificateDuplex/MutualSslNegotiated"的区别。我尝试使用其他选项,但得到了相同的结果。
  • 如何调用 WCF 服务?如何在客户端添加证书?你能指定客户端绑定、配置等吗?您可以使用 Fiddler 或 Soap UI 等工具来实际查看对您的服务进行的调用
  • 生产环境会有java客户端,但目前为了测试,我在c#中使用System.Net.HttpWebRequest类向WCF服务器发出请求,我将证书作为webRequest添加到客户端.ClientCertificates.Add(clientCert).
  • 我不知道您对此有多满意,但您可以轻松地使用 WPF 或 Winforms 客户端 - 将 WCF 服务引用添加到它 - 在配置中包含证书 - 开始 TracingAndLogging 在客户端,并检查服务是否实际被调用。您提到的警告通常是由于缺少绑定扩展而发生的。到目前为止,我建议您删除相互证书要求,查看服务是否使用基本配置工作,然后逐步添加这些配置。

标签: wcf security authentication ssl


【解决方案1】:

对我有用的代码如下:

String port = 443;
String certificateSubject = "Mymachine";
String urlString = String.Format("https://{0}:{1}/",System.Net.Dns.GetHostEntry("").HostName, port);
Uri httpUrl = new Uri(urlString);
ServiceHost host = new WebServiceHost(typeof(mynamespace.myclass), httpUrl);

WebHttpBinding wsBinding = new WebHttpBinding();
wsBinding.Security.Mode = WebHttpSecurityMode.Transport;
wsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

host.Credentials.ServiceCertificate.SetCertificate(
                                                    StoreLocation.LocalMachine,
                                                    StoreName.My,
                                                    X509FindType.FindBySubjectName,
                                                    certificateSubject);


host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
host.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new MyX509CertificateValidator();

host.AddServiceEndpoint(typeof(myinterface), wsBinding, httpUrl);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    相关资源
    最近更新 更多