WCF - wsHttpBinding with UserName Autentication and Message - 错误消息“处理消息中的安全令牌时发生错误" [英] WCF - wsHttpBinding with UserName Autentication and Message - error message "An error occurred when processing the security tokens in the message"

查看:22
本文介绍了WCF - wsHttpBinding with UserName Autentication and Message - 错误消息“处理消息中的安全令牌时发生错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create WCF applications with username authentication. And this error occurring.

这是服务配置:

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

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

When the client makes request for service triggers the following error:

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

InnerException.Message: An error occurred when processing the security tokens in the message.

消息:收到来自另一方的不安全或不正确保护的故障.故障代码和详细信息参见内部的FaultException.

Message: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.

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

推荐答案

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

This response can happen for a several reasons. The two that I suspect the most are:

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

  1. You are sending the incorrect client credentials to the server. I see you are sending a UserName and Password as "xxx". However, your server is expecting a UserName of "yaron". This is the expected response from the server in that case.

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

The client machine has an invalid time or one that is more than 5 minutes different from the server. The default MaxClockSkew value for the WSHttpBinding is 5 minutes.

这篇关于WCF - wsHttpBinding with UserName Autentication and Message - 错误消息“处理消息中的安全令牌时发生错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆