情况 :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。
问题:
任何帮助,将不胜感激!
最佳答案
以下代码片段可让您在 TransportSecurityBindingElement 上设置 maxClockSkew。我的解决方案是在 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;
}
关于silverlight - 如何修复 HTTPS Silverlight 应用程序上下文中的 WCF maxClockSkew 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971785/