简体   繁体   English

WCF-具有用户名确认和消息的wsHttpBinding-错误消息“处理消息中的安全令牌时发生错误”

[英]WCF - wsHttpBinding with UserName Autentication and Message - error message “An error occurred when processing the security tokens in the message”

I'm trying to create WCF applications with username authentication. 我正在尝试使用用户名身份验证创建WCF应用程序。 And this error occurring. 并发生此错误。

This is the service configuration: 这是服务配置:

Web.config Web.config文件

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Jsl.BureauInf.Services.BureauInfSVC" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="wsHttpBinding" contract="Jsl.BureauInf.Contracts.ICliente" bindingConfiguration="ServiceBinding"/>
        <endpoint address="" binding="wsHttpBinding" contract="Jsl.BureauInf.Contracts.IMotorista"  bindingConfiguration="ServiceBinding"/>
        <endpoint address="" binding="wsHttpBinding" contract="Jsl.BureauInf.Contracts.ITransportadora" bindingConfiguration="ServiceBinding"/>
        <endpoint address="" binding="wsHttpBinding" contract="Jsl.BureauInf.Contracts.IVeiculo" bindingConfiguration="ServiceBinding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="ServiceBinding">
          <security mode="Message">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="True"/>
          <serviceCredentials>
            <serviceCertificate findValue="Uareubinf"
                  storeLocation="LocalMachine"
                  storeName="My"
                  x509FindType="FindBySubjectName" />
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                customUserNamePasswordValidatorType="Jsl.BureauInf.Security.Autenticacao, Jsl.BureauInf.Security" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.net>
    <defaultProxy useDefaultCredentials="true"/>
  </system.net>
</configuration>

Class Autentication 类别强化

namespace Jsl.BureauInf.Security
{
public class Autenticacao : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName != "yaron")
            throw new SecurityTokenException("Unknown Username or Password");
    }
}
}

Client 客户

    BureauService.TransportadoraClient service = new BureauService.TransportadoraClient();
    service.ClientCredentials.UserName.UserName = "xxx";
    service.ClientCredentials.UserName.Password = "xxx";
    service.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

    BureauService.RESPOSTADTC rep = service.ConsultarTransportadora(consulta);

When the client makes request for service triggers the following error: 当客户端发出服务请求时,将触发以下错误:

InnerException.Message: An error occurred when processing the security tokens in the message. InnerException.Message:处理消息中的安全性令牌时发生错误。

Message: An unsecured or incorrectly secured fault was received from the other party. 消息:从另一方收到不安全或不正确的安全故障。 See the inner FaultException for the fault code and detail. 有关错误代码和详细信息,请参见内部FaultException。

What should I do to fix this error? 我该怎么办才能解决此错误?

This response can happen for a several reasons. 可能由于多种原因而发生此响应。 The two that I suspect the most are: 我最怀疑的两个是:

  1. You are sending the incorrect client credentials to the server. 您正在将错误的客户端凭据发送到服务器。 I see you are sending a UserName and Password as "xxx" . 我看到您发送的UserNamePassword"xxx" However, your server is expecting a UserName of "yaron" . 但是,您的服务器期望UserName"yaron" This is the expected response from the server in that case. 在这种情况下,这是服务器的预期响应。

  2. The client machine has an invalid time or one that is more than 5 minutes different from the server. 客户端计算机的无效时间或与服务器相差5分钟以上的无效时间。 The default MaxClockSkew value for the WSHttpBinding is 5 minutes. WSHttpBinding的默认MaxClockSkew值为5分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM