How to make WCF service server-client time difference independent?

后端 未结 2 791
感动是毒
感动是毒 2021-01-02 16:17

While accessing WCF service from a test client, I am getting following exception:

System.ServiceModel.Security.MessageSecurityException: An unsecured or inco         


        
相关标签:
2条回答
  • 2021-01-02 16:20

    I had the same issue and followed all recommendations. But my fault was that I changed only server configuration, whereas I had to change client configuration too.

    This is my config without maxClockSkew

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
      <connectionStrings>
        <!-- -->
      </connectionStrings>
      <system.serviceModel>
        <bindings>
          <wsHttpBinding>
            <binding name="WSHttpBinding_IHotLine1" closeTimeout="00:10:00"
              openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
              maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
              <security>
                <message clientCredentialType="UserName" />
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
        <client>
          <endpoint address="localhost:6767/blabla.svc" binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IHotLine1" contract="ServiceReference2.IHotLine"
            name="WSHttpBinding_IHotLine1">
            <identity>
              <certificate encodedValue="====encodedvalue===" />
            </identity>
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration>
    

    And updated with clock skew

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
      <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="WSHttpBinding_IHotLine1">
              <transactionFlow/>
              <security authenticationMode="SecureConversation"
    messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
                <localClientSettings maxClockSkew="01:30:00" />
                <localServiceSettings maxClockSkew="01:30:00" />
                <secureConversationBootstrap authenticationMode="UserNameForSslNegotiated"
    messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" >
                  <localClientSettings maxClockSkew="01:30:00" />
                  <localServiceSettings maxClockSkew="01:30:00" />
                </secureConversationBootstrap>
              </security>
              <textMessageEncoding/>
              <httpTransport maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
            </binding>
          </customBinding>
        </bindings>
        <client>
    
          <endpoint address="http://localhost:6767/blabla.svc" binding="customBinding"
            bindingConfiguration="WSHttpBinding_IHotLine1" contract="ServiceReference2.IHotLine"
            name="WSHttpBinding_IHotLine1">
            <identity>
              <certificate encodedValue="====encodedvalue===" />
            </identity>
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration> 
    
    0 讨论(0)
  • 2021-01-02 16:23

    There can be multiple reasons for this error, the common one is related to server not authenticating client for variosu reasons (maybe not on the same domain). To determine the exact reasons turn on wcf trace and see what errors it shows in red. What you're looking for is the inenr exception which is a little hidden in the trace UI, it is in the right side in the middle down the tree.

    Here is how to set a clock skew:

    <security authenticationMode="...">
               <localClientSettings maxClockSkew="00:07:00" />
               <localServiceSettings maxClockSkew="00:07:00" />
               <secureConversationBootstrap>
                  <localClientSettings maxClockSkew="00:30:00" />
                  <localServiceSettings maxClockSkew="00:30:00" />
               </secureConversationBootstrap>
    </security>
    

    Note skew can only be defined on a custom binding. Since you use WSHttpBinding you need to convert it to a custom binding which can be easily be done online via WCF binding converter.

    0 讨论(0)
提交回复
热议问题