验证消息安全性时发生错误

28

在尝试调用WCF服务时,我收到了以下消息:“验证消息安全性时发生错误。”

当我删除自定义身份验证时,服务可以正常工作。但是我无法确定我在web.config中配置的内容有什么问题。希望能提供任何见解。

  <system.serviceModel>
     <services>
        <service behaviorConfiguration="NAThriveExtensions.nableAPIBehavior"
          name="NAThriveExtensions.nableAPI">
           <endpoint 
             address="" 
             binding="basicHttpBinding" 
             bindingConfiguration="basicHttpBinding_Secure"
             contract="NAThriveExtensions.InableAPI">
           </endpoint>
           <endpoint 
             address="mex" 
             binding="mexHttpsBinding" 
             contract="IMetadataExchange" />
        </service>
     </services>
     <behaviors>
        <serviceBehaviors>
          <behavior name="NAThriveExtensions.nableAPIBehavior">
            <serviceMetadata httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
            <serviceCredentials>
              <userNameAuthentication 
                userNamePasswordValidationMode="Custom" 
              customUserNamePasswordValidatorType= "NAThriveExtensions.Authentication, NAThriveExtensions" />
            </serviceCredentials>
          </behavior>
        </serviceBehaviors>
     </behaviors>
     <bindings>
       <basicHttpBinding>
         <binding name="basicHttpBinding_Secure">
           <security mode="TransportWithMessageCredential">
             <message clientCredentialType="UserName"/>
           </security>
         </binding>
       </basicHttpBinding>
     </bindings>
  </system.serviceModel>
8个回答

41

我遇到了与您相同的错误消息,原因是我的工作站机器和托管WCF服务的服务器之间存在时间差异。服务器比我的机器慢大约10分钟,而WCF安全性似乎不太喜欢这种情况。

为了找到根本问题,我在服务器的配置文件中启用了serviceSecurityAuditing。请添加以下内容到您服务的configuration/system.serviceModel/behaviors/serviceBehaviors/behavior部分:

<serviceSecurityAudit 
    auditLogLocation="Application" 
    serviceAuthorizationAuditLevel="Failure" 
    messageAuthenticationAuditLevel="Failure" 
    suppressAuditFailure="true"/>
以下网站有助于解决此问题:

http://blogs.microsoft.co.il/blogs/urig/archive/2011/01/23/wcf-quot-an-error-occurred-when-verifying-security-for-the-message-quot-and-service-security-audit.aspx


10
非常好的诊断提示!谢谢分享。 - Tedford
1
这真是一个很好的提示。就像这里所描述的那样,我似乎缺少了一个DLL:https://dev59.com/questions/f2Yr5IYBdhLWcg3wAFg0#iaafEYcBWogLw_1bJt-M - ArieKanarie
3
我希望我能给这个点赞多次!花了半天时间才找到真正的错误信息。 - L_7337

20
另一个导致此消息的原因是您的某些机器时间未同步。WCF默认允许五分钟的时间差;超过这个值,如果不同步则会抛出错误。
解决方案是同步所有的机器。time.windows.com以不工作而著称,建议使用其他方法。(如果您在公司环境中,本地域控制器可能是正确的选择。)

4
非常感谢!我一直在苦思冥想这个问题的原因,快让我抓狂了! - CaptainStealthy
很高兴不只有我一个人这样想,@Stealth22 :) - ashes999
3
我的服务器和客户端使用的是同一台时钟。客户端时钟自动配置,而服务器时钟是手动设置的。我在服务器上激活了“自动设置时间”的功能,问题就解决了。 - Oktay

7

这实际上是在消费方面出了问题,而不是服务本身。Software AG的webMethods 8正在消费此服务器,但没有添加安全处理程序到服务中,因此凭据未被添加到标头中,从而导致上述错误。


5
如何确定这是否确实是问题所在? - Maritim
@Maritim: 好问题。我知道的最好方法是查看原始的HTTP请求 - 例如,如果它尚未被记录,则通过记录来查看它:缺失SOAP头的(元素)应该就会显现出来。 - J0e3gan

1
我遇到了类似的问题。我使用本地时间构建我的日期时间格式化字符串,但我的服务/服务器需要GMT时间。我需要获取GMT时间(JAVA):
final Date currentTime = new Date();    
final SimpleDateFormat sdf = 
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(currentTime));

0

我遇到了同样的错误,但以上方法都没有对我有所帮助。

最终我发现问题出在父级 web.config 文件中的 connectionStrings(我的服务部署在管理站点的子应用程序中)。

是的,听起来很荒谬,但只要我在父级 web.config 中用 location 元素包装连接字符串,一切就开始正常工作了。

为了清晰起见,在父级 web.config 中,我将以下内容进行了更改:

<connectionStrings>
    <add name="..." />
</connectionStrings>

到这里

<location path="." inheritInChildApplications="false">
    <connectionStrings>
        <add name="..." />
    </connectionStrings>
</location>

请注意,此错误还导致了这个非常无用的serviceSecurityAudit日志消息:

消息认证失败。
服务:...
操作:http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT
客户端标识:
活动 ID:
ArgumentNullException:值不能为 null。
参数名:manager


0

我在我的IIS 7.5服务器上遇到了同样的错误。我忘记为证书的私钥添加应用程序池虚拟帐户(例如IIS AppPool\ASP.NET v4.0)的读取权限。

值得注意的是,在测试各种帐户和权限的组合时,我注意到一旦检索到密钥,就需要重新启动应用程序池才能失去对密钥的访问。

(0x80131501-验证消息安全性时发生错误。)


0

我遇到了同样的错误。我忘记在成员资格数据库aspnetdb上添加读取权限给(IIS APPPOOL\DefaultAppPool)。

消息认证失败。服务:....

操作:http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT

客户端身份:

活动 ID:

SqlException:无法打开请求的登录名为“aspnetdb”的数据库。

用户“IIS APPPOOL\DefaultAppPool”的登录失败。


-4
用户名和密码是您连接的服务器,而不是您系统登录的用户名和密码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接