silverlight - 如何修复 HTTPS Silverlight 应用程序上下文中的 WCF maxClockSkew 问题?

标签 silverlight wcf security binding https

情况 :Silverlight 4 应用程序通过 WCF 使用 basicHttpBinding 和 HTTPS 与服务器组件通信。

这是服务器端使用的绑定(bind):

<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 上。

应用程序在本地运行时运行流畅。

但是,我们现在有外部用户连接到我们的应用程序。
他们中的一些人遇到了困难,查看服务器日志,我们发现了这个错误:

“消息安全异常”
安全时间戳已过时,因为它的过期时间 ('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

它在现有绑定(bind)上使用 CustomBinding,并在自定义绑定(bind)的 SecurityBindingElement 上设置 MaxClockSkew 属性。
我们实现了这个解决方案,但是将 SymmetricSecurityBindingElement 更改为 TransportSecurityBindingElement,因为我们与 Silverlight 进行安全通信的绑定(bind)
是带有 HTTPS 的 basicHttpBinding。

网络上的几篇文章(包括上面列出的这篇 MSDN 文章)显示了另外将 maxClockSkew 属性设置为从 获取的引导元素的代码片段。保护 token 参数 .
自从 以来,我从未成功地将这部分应用到我们的代码中。 TransportSecurityBindingElement 似乎没有任何 ProtectionTokenParameters。

这是我们用 maxClockSkew 包装绑定(bind)的代码:
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 绑定(bind)问题!

这是它在 TransportSecurityBindingElement 的上下文中搜索 token 参数的部分:
//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。

问题:
  • 我们的绑定(bind)在 SL + WCF + HTTPS 上下文中是否不正确?
  • 找不到任何方法在 TransportSecurityBindingElement 中的引导元素上设置 maxClockSkew 是否正常?
  • 我们是唯一一家使用 HTTPS Silverlight 应用程序的公司,其客户可能不在同一时间(+- 5 分钟偏移)?
  • 为什么修复这种微不足道的配置似乎是一种冒险?

  • 任何帮助,将不胜感激!

    最佳答案

    以下代码片段可让您在 TransportSecurityBindingElement 上设置 maxClockSkew。我的解决方案是在 http 和 https 上下文中运行的 Outlook 加载项 + WCF,因此虽然与您的上下文不同,但它相似。

  • 你的绑定(bind)在我看来是正确的。
  • 这是代码片段
    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;
    } 
  • 时钟偏差仅在您使用 UserName 客户端凭据时很重要,并且某些用户要么喜欢他们的计算机时钟不是正确的时间,要么他们不在乎
  • 是的,WCF 配置始终是您不愿做的冒险。
  • 关于silverlight - 如何修复 HTTPS Silverlight 应用程序上下文中的 WCF maxClockSkew 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971785/

    相关文章:

    wcf - WCF 服务中的 *Result 和 *ResultSpecified 参数?

    c# - 升级到 CaSTLe 3.0 后出现 ComponentResolutionException

    asp.net - 将 PayPal 凭据存储在共享主机上的 Web 应用程序 (asp.net) 中

    asp.net - EncryptByKey 与 EncryptByPassPhrase?

    silverlight - 通过浏览器访问专有 USB 设备……可能吗?银光也许? (Java 暂时不包括在内)

    Silverlight 4 文本框不显示所有内容

    c# - 如何返回 HTTP 429?

    c# - 将特定于 Silverlight 的 System.Xml.Linq dll 与非 Silverlight System.Xml.Linq dll 混合

    silverlight - Windows Phone 7 silverlight 用户控制 : data binding not working on custom property

    php - 实现 session 以保持用户登录,安全问题