0

我创建了一个启用了消息安全模式的 wcf 服务这是我的服务

    [ServiceContract]
    public interface IMessagingServices
    {
        [OperationContract]
        string SendMessage(string from, string to, string message);

        [OperationContract]
        List<Message> GetMessages(string from, string to);

        [OperationContract]
        int DeleteMessages(int[] idList);
    }
}

这就是它的配置

<bindings>
      <wsHttpBinding>
        <binding name="wsBinding1">
          <security mode="Message">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>



<services>


  <service name="MessagingServices" behaviorConfiguration="SecureServiceBehavior" >
    <endpoint address="" binding="wsHttpBinding" contract="IMessagingServices" bindingConfiguration="wsBinding1" />
    <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetaDataExchange"/>-->
  </service>

</services>


<behaviors>

  <endpointBehaviors>
    <behavior name="httpBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>

    <behavior name="SecureServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <serviceCertificate findValue="KServic.local" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="AuthenticationHandler, mynamespace" />
      </serviceCredentials>
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

一切似乎都很好,我可以在 VS 2010 的添加服务引用中添加服务,并且代理已连续创建,但在客户端尝试调用服务操作时出现错误,这是客户端代码

ServiceReference1.MessagingServicesClient mscClient = new MessagingServicesClient();

           // mscClient.Open();

            mscClient.ClientCredentials.UserName.UserName = "test";
            mscClient.ClientCredentials.UserName.Password = "test";
            mscClient.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "KService.local");

             // error is here
            var msg = mscClient.SendMessage("rnd.test", "rnd.test", "Hello brother!");

            mscClient.Close();

这是错误

传出消息的身份检查失败。预期的身份是'身份(http://schemas.xmlsoap.org/ws/2005/05/identity/right/possessproperty:http://schemas.xmlsoap.org/ws/2005/05/identity/claims/thumbprint )' 用于 'http://192.168.100.24:16027/MessagingServices.svc' 目标端点。

这里有什么问题?我该如何解决这个问题?

4

1 回答 1

0

使用消息安全时,您应该在客户端设置端点标识。
您提供的代码设置客户端凭据,而不是端点身份。

如果您想通过 .config 文件或类设置身份,请参阅WCF 配置架构中<identity>的元素,如果您想在代码中设置身份:<certificateReference>X509CertificateEndpointIdentity

var certificate = ...; // load X509Certificate2 instance from the X509Store
var address = new EndpointAddress(uri, new X509CertificateEndpointIdentity(certificate));

请注意,该服务证书必须在客户端使用之前进行验证。有关详细信息,请参阅<authentication> of <serviceCertificate>元素页面。

于 2012-09-10T06:56:29.423 回答