.NET 4.5 C#应用时钟不同步配置timeToleranceInSeconds后报错求助
问题概述
基于.NET 4.5的C#应用中,因服务器时钟滞后8小时且无法修改,触发WSE065安全头处理错误,错误信息如下:
An error was discovered processing the
header ---> WSE065: Creation time of the timestamp is in the future. This typically indicates lack of synchronization between sender and receiver clocks. Make sure the clocks are synchronized or use the timeToleranceInSeconds element in the microsoft.web.services3 configuration section to adjust tolerance for lack of clock synchronization.
尝试添加配置后出现配置节加载异常,需修正配置并排查相关问题。
配置错误修复
你提供的配置存在标签闭合错误,同时时间容忍度数值设置过大,修正后的配置如下:
<configSections> <section name="microsoft.web.services3" type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <microsoft.web.services3> <tokenIssuer> <ttlInSeconds value="900000" /> </tokenIssuer> <security> <defaultTtlInSeconds value="30000" /> <timeToleranceInSeconds value="28800" /> <!-- 对应8小时的秒数,精准匹配时钟偏差 --> </security> </microsoft.web.services3>
- 修正点1:将末尾错误的
<web.services3>改为正确的闭合标签</microsoft.web.services3> - 修正点2:把
timeToleranceInSeconds调整为8小时对应的28800秒,避免过度设置引发其他问题
额外排查与代码补充
程序集引用检查
确认项目已正确引用Microsoft.Web.Services3程序集,版本需与配置中指定的3.0.0.0一致。配置位置验证
若为Web应用,需确保配置代码放在web.config的<configuration>根节点下,不要嵌套在其他节点(如<system.web>)内。代码层面手动设置(备选方案)
若配置修改后仍有问题,可在代码中直接设置时间容忍度:
using Microsoft.Web.Services3; using Microsoft.Web.Services3.Security; // 初始化策略并设置时间容忍度 var policy = new Policy(); var securityAssertion = new UsernameOverTransportAssertion(); securityAssertion.Timestamp.Tolerance = TimeSpan.FromHours(8); policy.Assertions.Add(securityAssertion); // 将策略应用到Web服务客户端 yourWebServiceClientInstance.SetPolicy(policy);
内容的提问来源于stack exchange,提问作者Ali

