【发布时间】:2019-09-11 10:44:06
【问题描述】:
我有一个带有 net.tcp 端点的 WCF 服务,它使用自定义 usernamePassswordValidator、自定义授权和 TransportWithMessageCredential,凭据类型为“用户名”(见下文)。 服务器和客户端工作正常 - 除非服务器和客户端计算机之间的时间偏差超过 5 分钟。
现在我尝试在代码中设置最大偏移时间。我尝试从 MSDN 改编用于 WSHttpBindings 的代码 sn-ps 并在服务器和客户端上使用自定义绑定:
binding = GetSecuredBindingFromServerOrClient();
CustomBinding myCustomBinding = new CustomBinding(binding);
var security = myCustomBinding.Elements.Find<TransportSecurityBindingElement>(); // TransportSecurityBindingElement or SecurityBindingElement
security.LocalClientSettings.MaxClockSkew = timeSkew;
security.LocalServiceSettings.MaxClockSkew = timeSkew;
security.LocalServiceSettings.DetectReplays = false;
security.IncludeTimestamp = false;
// on client: use this custom binding in channel factory
var channelFactory = new ChannelFactory<ICheckerService>(customBinding, someAddress);
// on server: Update binding with customBinding
endpoint.Binding = myCustomBinding;
当时间偏差超过 5 分钟(默认值)时,连接仍然失败并显示 MessageSecurityException。我还将 IncludeTimestamp 设置为 false 或 true,但它们都没有改善这种情况。
服务器行为是:
<behavior name="customUserNamePasswordSecurityBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MySecurity.BasicAuthenticationValidator, MySecurity.Services"/>
</serviceCredentials>
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="Security.CustomAuthorizationPolicy, MySecurity.Services"/>
</authorizationPolicies>
</serviceAuthorization>
</behavior>
那么端点绑定是:
<binding name="tcpUserNameAuthentication">
<reliableSession enabled="true"/>
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
有没有人根据上面的 TransportWithMessageCredential 和 net.tcp 配置得到时间偏差?还是有基本的误解?
【问题讨论】:
-
使服务器成为客户端的时间服务器。然后两者将同时发生。
-
不幸的是这是不可能的,因为 n 个客户端连接到 m 个服务器实现。最好的选择可能是让客户端和服务器使用时间服务器,但我们不能确保(至少不能保证 5 分钟 wcf 的默认值)。
标签: c# wcf wcf-security nettcpbinding net-tcp