2010-10-19 16 views
6

情況:Silverlight 4應用程序使用basicHttpBinding和HTTPS通過WCF與服務器組件進行通信。如何修復HTTPS Silverlight應用程序上下文中的WCF maxClockSkew問題?

這裏是結合使用的服務器端:

<basicHttpBinding> 
<binding name="DefaultSecuredBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> 
     <readerQuotas maxDepth="50" maxArrayLength="2147483647" maxStringContentLength="2147483647" /> 
     <security mode="TransportWithMessageCredential"> 
     <message clientCredentialType="UserName"/> 
     <transport clientCredentialType="None" proxyCredentialType="None"/> 
     </security> 
    </binding> 
</basicHttpBinding> 

注意,我們使用TransportWithMessageCredential爲安全模式。 證書已正確安裝在IIS上。

本地運行時,應用程序運行平穩。

但是,我們現在有外部用戶連接到我們的應用程序。 他們中的一些遇到困難,並期待服務器日誌中,我們發現了這個錯誤:

「MessageSecurityException」 安全時間戳是陳舊的,因爲它的到期時間(「2010-10-18T22:37: 58.198Z')已經過去了。當前時間爲'2010-10-18T22:43:18.850Z',允許時鐘偏差爲'00:05:00'。

我們對網絡主題(StackoverFlow & Google和Bing)進行了常規研究,以瞭解更多關於該主題的內容。 我們聯繫了用戶,以確保他們與我們的服務器的時間偏移,後來得到確認。

這MSDN文章是開始: http://msdn.microsoft.com/en-us/library/aa738468.aspx

其中用在現有的綁定CustomBinding並設置自定義綁定的SecurityBindingElement的MaxClockSkew財產。 我們實現了此解決方案,但是將SymmetricSecurityBindingElement更改爲TransportSecurityBindingElement,因爲我們與Silverlight 進行安全通信的綁定是basicHttpBinding與HTTPS。

幅材(包括上面列出的這MSDN文章)表明,另外的maxClockSkew屬性設置爲從ProtectionTokenParameters截取的引導元件的代碼段上的文章。 我從來沒有成功將這部分應用到我們的代碼中,因爲TransportSecurityBindingElement似乎沒有任何ProtectionTokenParameters

這裏是我們的代碼來包裝與maxClockSkew綁定:

protected virtual System.ServiceModel.Channels.Binding WrapClockSkew(System.ServiceModel.Channels.Binding currentBinding) 
    { 
     // Set the maximum difference in minutes 
     int maxDifference = 300; 

     // Create a custom binding based on an existing binding 
     CustomBinding myCustomBinding = new CustomBinding(currentBinding); 

     // Set the maxClockSkew 
     var security = myCustomBinding.Elements.Find<TransportSecurityBindingElement>(); 
     if (security != null) 
     { 
      security.LocalClientSettings.MaxClockSkew = TimeSpan.FromMinutes(maxDifference); 
      security.LocalServiceSettings.MaxClockSkew = TimeSpan.FromMinutes(maxDifference); 
     } 


     return myCustomBinding; 
    } 

的「security.LocalClientSettings」可能是無用的在這裏,因爲這個代碼是在服務器端。

此代碼沒有做的伎倆,當我們有超過5分鐘的服務器差異時,我們仍然在服務器上有相同的錯誤信息。 我還記得,我們沒有應用MSDN代碼片段的引導技巧..所以我們繼續我們的網頁搜索的主題。

我們發現了一個整潔的wcf行爲,我們認爲這可以解決我們的問題。

它看起來像處理Bootstrap綁定問題!

這裏是它在一個TransportSecurityBindingElement的上下文搜索令牌參數部分:

//If the securityBindingElement's type is TransportSecurityBindingElement 
if (securityBindingElement is TransportSecurityBindingElement) 
{ 
foreach (SecurityTokenParameters securityTokenParameters in 
    securityBindingElement.EndpointSupportingTokenParameters.Endorsing) 
{ 
    //Gets it from the EndpointSupportingTokenParameters.Endorsing property 
    if (securityTokenParameters is SecureConversationSecurityTokenParameters) 
    { 
     secureConversationSecurityTokenParameters = 
      securityTokenParameters as SecureConversationSecurityTokenParameters; 

     break; 
    } 
} 
} 

注意'securityBindingElement.EndpointSupportingTokenParameters.Endorsing' ... 在我們的情況(basicHttpBinding的,TransportWithMessageCredential,Https ...),但這個集合是空的!

所以,沒辦法檢索securityTokenParameters,從而無法設置maxClockSkew。

問題:

  • 我們是不正確的綁定在SL + WCF + HTTPS背景?

  • 是正常的,沒有找到任何方法來設置maxClockSkew在TransportSecurityBindingElement的引導元素?

  • ,我們在做客戶HTTPS Silverlight應用程序可能不是確切的同一時間的唯一公司(與+ - 5分鐘偏移量)?

  • 爲什麼它似乎是相當冒險來解決這些瑣碎的配置嗎?

任何幫助將不勝感激!

回答

5

配置樣品下面的代碼片段,您可以設置maxClockSkew上TransportSecurityBindingElement。我的解決方案是在http和https上下文中運行的Outlook加載項+ WCF,因此雖然與您的類似的上下文不同。

  • 您的綁定看起來是正確的我。
  • 下面的代碼片段

    WSHttpBinding wsSecureBinding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential, false); 
    wsSecureBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
    wsSecureBinding.Security.Message.EstablishSecurityContext = true; 
    wsSecureBinding.Security.Message.NegotiateServiceCredential = true; 
    wsSecureBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 
    wsSecureBinding.ReaderQuotas.MaxStringContentLength = 500000; 
    wsSecureBinding.ReceiveTimeout = 
    wsSecureBinding.SendTimeout = new TimeSpan(0, 5, 0); 
    CustomBinding secureCustomBinding = new CustomBinding(wsSecureBinding); 
    TimeSpan clockSkew = new TimeSpan(0, 15, 0); 
    
    TransportSecurityBindingElement tsecurity = secureCustomBinding.Elements.Find(); 
    SecureConversationSecurityTokenParameters secureTokenParams = (SecureConversationSecurityTokenParameters)tsecurity.EndpointSupportingTokenParameters.Endorsing.OfType().FirstOrDefault(); 
    if (secureTokenParams != null) 
    { 
        SecurityBindingElement bootstrap = secureTokenParams.BootstrapSecurityBindingElement; 
        // Set the MaxClockSkew on the bootstrap element. 
        bootstrap.LocalClientSettings.MaxClockSkew = clockSkew; 
        bootstrap.LocalServiceSettings.MaxClockSkew = clockSkew; 
    }
  • 時鐘偏差與您使用用戶名客戶端憑據和一些用戶要麼像不是自己的計算機時鐘正確的時間,或者他們不關心

  • 僅事項
  • 是的,WCF配置永遠是你不願意做的冒險。
+0

這聽起來很有希望。我會試試這個,並會告訴你它是否解決了我們的問題。同時,我們決定從消息中刪除時間信息,因爲我們不需要它。如果沒有發送時間信息,則不會檢查時鐘偏差驗證。非常感謝! – 2011-01-07 04:04:26

+0

我認爲如果你能提供有關如何刪除時間信息的英特爾公司,很多人會感激不盡。例如。現在我可以從中受益。 :) 提前致謝! – Cornelius 2011-06-20 11:24:49

+0

你想要刪除什麼時間信息?時鐘偏斜時間信息?如果是,那麼您需要爲clientCredentialType屬性選擇一個備用值,因爲時鐘歪斜用於保證用戶名和密碼的安全。 – 2011-06-20 13:36:32

相關問題