WCF项目中客户端与服务器时间问题步无法连接的解决方案 转

新项目中使用了WCF 实现客户端与服务器的通讯。c/s架构,客户端采用winfrom实现。项目试运行期间,很多用户都抱怨系统总是无法正常登录。

查看日志发现如下异常信息:

System.ServiceModel.Security.MessageSecurityException: 从另一方收到未进行安全处理或安全处理不正确的错误。有关错误代码和详细信息,请
参阅内部 FaultException。 ---> System.ServiceModel.FaultException: 消息中至少有一个安全令牌无法验证。

      WCF通讯采用了UserName安全认证方式,用Google大神搜索一番,上述异常多是因为客户端与服务器端时间不同步所引起的,WCF所提供的几种Binding客户端和服务端所允许的时间差可以是5分钟.

原因找到了,下面就是如何解决了,网上大多数方法是使用 命令:net time \\IP地址或服务器名 /set /yes 去同步客户端的时间,这种方式我直接给Pass掉了,原因如下:

      通过跟用户的交流发现很多用户是故意将时间提前或推后十几分钟的,原因很多就不详细列具了。

继续找其它解决方案,在国外一个论坛上发现可以使用customBinding 修改允许的最大时间偏差。let's try it!

修改前的配置文件(删减了一些配置节)如下:
01
    <system.serviceModel>
02
    <client/>
03
    <services>
04
               <service name="userService">
05
                <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding_user"
06
                 contract="Iuser"/>
07
     </service>
08
    <bindings>
09
       <wsHttpBinding>
10
        <binding name="Binding_user">
11
         <security mode="Message">
12
          <transport/>     
13
          <message clientCredentialType="UserName" />
14
         </security>
15
        </binding>
16
       </wsHttpBinding>
17
      </bindings>
18
    </system.serviceModel>
1
    将wsHttpBinding替换为customBinding后配置文件如下:
01
    <system.serviceModel>
02
    <client/>
03
    <services>
04
          <service name="userService">
05
                <endpoint address="" binding="customBinding" bindingConfiguration="MyCustomBinding"
06
                 contract="Iuser"/>
07
      </service>
08
      </services>
09
      <bindings>
10
     <customBinding>
11
           <binding name="MyCustomBinding">
12
               <transactionFlow />
13
               <security authenticationMode="UserNameForSslNegotiated">
14
                   <secureConversationBootstrap>
15
                       <localClientSettings maxClockSkew="00:59:00" />
16
                       <localServiceSettings maxClockSkew="00:59:00" />
17
                   </secureConversationBootstrap>
18
                   <localClientSettings maxClockSkew="00:59:00" />
19
                   <localServiceSettings maxClockSkew="00:59:00" />
20
               </security>
21
               <textMessageEncoding>
22
                   <readerQuotas maxStringContentLength="500000"/>
23
               </textMessageEncoding>
24
               <httpTransport maxReceivedMessageSize="10485760" maxBufferPoolSize="524288" />
25
           </binding>
26
       </customBinding>
27
    </bindings>
28
    </system.serviceModel>
1
    这里将充许的最大时间偏差改为59分钟。
1
    到这里还不算完,以上只是修改的服务端配置文件,在客户端的app.config文件中同样要修改,客户端配置修改后如下:
01
    <system.serviceModel>
02
          <bindings>
03
        <customBinding>
04
                  <binding name="MyCustomBinding" closeTimeout="00:01:00"
05
                      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
06
                       >
07
                      <transactionFlow />
08
                      <security authenticationMode="UserNameForSslNegotiated">
09
                          <secureConversationBootstrap>
10
                              <localClientSettings maxClockSkew="00:59:00" />
11
                              <localServiceSettings maxClockSkew="00:59:00" />
12
                          </secureConversationBootstrap>
13
                          <localClientSettings maxClockSkew="00:59:00" />
14
                          <localServiceSettings maxClockSkew="00:59:00" />
15
                      </security>
16
                      <textMessageEncoding>
17
                          <readerQuotas maxStringContentLength="500000"/>
18
                      </textMessageEncoding>
19
                      <httpTransport maxReceivedMessageSize="10485760" maxBufferPoolSize="524288" />
20
                  </binding>
21
              </customBinding>
22
          </bindings>
23
          <client>
24
            <endpoint address="http://********/user.svc" binding="customBinding"
25
                      bindingConfiguration="MyCustomBinding" contract="Iuser"
26
                      name="CustomBinding_Iuser" />
27
           </client>
28
      </system.serviceModel>

posted on 2012-04-15 08:09  武胜-阿伟  阅读(1077)  评论(0编辑  收藏  举报