将NetTcpBinding与TransportWithMessageCredential一起使用时设置MaxClockSkew

时间:2019-04-26 20:09:42

标签: c# .net wcf wcf-binding wcf-security

我将WCF与NetTcpBinding一起使用,并且当使用TransportWithMessageCrediential的SecurityMode时,由于SymmetricSecurityBindingElement为null,因此无法设置MaxClockSkew。有谁知道使用TransportWithMessageCrediential SecurityMode时是如何做到的?

    private Binding GetNetTcpBinding()
    {
        long MaxReceivedMessageSize = 2147483647;
        int MaxStringContentLength = 2147483647;
        int MaxBytesPerRead = 2147483647;
        int MaxNameTableCharCount = 100000;
        var timeout = 5;
        var maxClockSkew = new TimeSpan(0, 30, 0);

        var binding = new NetTcpBinding
        {
            OpenTimeout = new TimeSpan(0, 0, timeout),
            SendTimeout = new TimeSpan(0, 0, timeout),
            ReceiveTimeout = new TimeSpan(0, 0, timeout),
            MaxReceivedMessageSize = MaxReceivedMessageSize,
            ReaderQuotas = new XmlDictionaryReaderQuotas
            {
                MaxArrayLength = MaxBytesPerRead,
                MaxBytesPerRead = MaxBytesPerRead,
                MaxNameTableCharCount = MaxNameTableCharCount,
                MaxStringContentLength = MaxStringContentLength,
                MaxDepth = MaxBytesPerRead
            }
        };

        binding.ReliableSession = new OptionalReliableSession
        {
            Enabled = true,
            InactivityTimeout = new TimeSpan(0, 0, timeout),
            Ordered = true
        };

        binding.Security = new NetTcpSecurity
        {
            Mode = SecurityMode.TransportWithMessageCredential,
            Message = new MessageSecurityOverTcp
            {
                ClientCredentialType = MessageCredentialType.Certificate,
                AlgorithmSuite = SecurityAlgorithmSuite.Basic256
            },
            Transport = new TcpTransportSecurity
            {
                ClientCredentialType = TcpClientCredentialType.Certificate,
                SslProtocols = SslProtocols.Tls12
            }
        };

        var customBinding = new CustomBinding(binding);

        var security =
            customBinding.Elements.Find<SymmetricSecurityBindingElement>();

        //  NOTE: This Always Returns NULL
        if (security != null)
        {
            security.LocalClientSettings.MaxClockSkew = maxClockSkew;
            security.LocalServiceSettings.MaxClockSkew = maxClockSkew;

            // Get the System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters 
            var secureTokenParams =
                (SecureConversationSecurityTokenParameters)security.ProtectionTokenParameters;

            // From the collection, get the bootstrap element.
            var bootstrap = secureTokenParams.BootstrapSecurityBindingElement;

            // Set the MaxClockSkew on the bootstrap element.
            bootstrap.LocalClientSettings.MaxClockSkew = maxClockSkew;
            bootstrap.LocalServiceSettings.MaxClockSkew = maxClockSkew;

            return customBinding;
        }

        return binding;
    }

我找不到任何解决方案,并且文档似乎已过时。仅在使用SecurityMode.Message时有效。

0 个答案:

没有答案