【问题标题】:WCF - wsHttpBinding with UserName Autentication and Message - error message "An error occurred when processing the security tokens in the message"WCF - 带有用户名身份验证和消息的 wsHttpBinding - 错误消息“处理消息中的安全令牌时发生错误”
【发布时间】:2014-05-13 12:55:22
【问题描述】:

我正在尝试使用用户名身份验证创建 WCF 应用程序。并且发生了这个错误。

这是服务配置:

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>

类认证

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");
    }
}
}

客户

    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);

当客户端请求服务时触发如下错误:

InnerException.Message:处理消息中的安全令牌时出错。

消息:从另一方收到不安全或不正确安全的故障。故障代码和细节见内部FaultException。

我应该怎么做才能修复这个错误?

【问题讨论】:

  • 当客户端上的时间与服务器上的时间相差太大时,可能会发生这种情况。默认时间偏差为 5 分钟。
  • 您真的在客户端发送凭据为“xxx”吗?还是您只是为了您的 sn-p 而删除它们?

标签: wcf wcf-binding wcf-security wcf-wshttpbinding


【解决方案1】:

出现此响应的原因可能有多种。我最怀疑的两个是:

  1. 您正在向服务器发送不正确的客户端凭据。我看到您正在发送UserNamePassword 作为"xxx"。但是,您的服务器需要 UserName"yaron"。在这种情况下,这是来自服务器的预期响应。

  2. 客户端计算机的时间无效或与服务器的时间相差超过 5 分钟。 WSHttpBinding 的默认 MaxClockSkew 值为 5 分钟。

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2018-04-11
    • 2020-05-19
    相关资源
    最近更新 更多