Howw to configure IssuedSecurityTokenParameters in .NET 6

76 Views Asked by At

We have our WCF services which requires custom binding and additional parameters. We have created its custom bindings and passed additional parameters in .NET 4.8 framework. Now we are migrating it to .NET 6. After upgrading the project getting two errors as "IssuedSecurityTokenParameters does not contains a definition AdditionalRequestParameters and IssuerMetadataAddress. We have added System.ServiceModel reference in our .NET 6 project. Code is as below

 public Binding CreateBinding(string customCode, string serverName, string msLogon)
    {
        var issuedSecurityTokenParameters = CreateIssuedSecurityTokenParameters(customCode, serverName, msLogon);

        var issuedTokenOverTransport = SecurityBindingElement.CreateIssuedTokenOverTransportBindingElement(issuedSecurityTokenParameters);
        issuedTokenOverTransport.MessageSecurityVersion = MessageSecurityVersion;
        issuedTokenOverTransport.LocalClientSettings.MaxClockSkew = TimeSpan.FromMinutes(5);

        SecurityBindingElement secureConversationBindingElement = SecurityBindingElement.CreateSecureConversationBindingElement(issuedTokenOverTransport);
        secureConversationBindingElement.MessageSecurityVersion = MessageSecurityVersion;

        BinaryMessageEncodingBindingElement binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement
        {
            MaxReadPoolSize = config.MaxReadPoolSize,
            MaxSessionSize = config.MaxSessionSize,
            MaxWritePoolSize = config.MaxWritePoolSize,
            ReaderQuotas =
            {
                MaxArrayLength = config.MaxArrayLength,
                MaxStringContentLength = config.MaxStringContentLength,
                MaxBytesPerRead = config.MaxBytesPerRead
            },
            CompressionFormat = config.CompressionFormat
        };

        TcpTransportBindingElement tcpTransportBindingElement = new TcpTransportBindingElement
        {
            MaxBufferPoolSize = config.MaxBufferPoolSize,
            MaxBufferSize = config.MaxBufferSize,
            ConnectionBufferSize = config.ConnectionBufferSize,
            MaxReceivedMessageSize = config.MaxReceivedMessageSize,
            TransferMode = TransferMode.Streamed

        };

        var streamSecurity = new SslStreamSecurityBindingElement { RequireClientCertificate = false };

        CustomBinding binding = new CustomBinding()
        {
            Elements =
                                            {
                                                secureConversationBindingElement,
                                                binaryMessageEncodingBindingElement,
                                                streamSecurity,
                                                tcpTransportBindingElement
                                            },

            Name = "tcpTransportWithIssuedTokenSecurity",
            Namespace = "http://fully-qualified-namespcae.com/tcp",
            CloseTimeout = config.CloseTimeout,
            OpenTimeout = config..OpenTimeout,
            ReceiveTimeout = config.ReceiveTimeout,
            SendTimeout = config.SendTimeout

        };

        return binding;
    }
    private IssuedSecurityTokenParameters CreateIssuedSecurityTokenParameters(string customCode, string serverName, string msLogon)
    {
        //var identity = EndpointIdentity.CreateUpnIdentity(config.ServerConfiguration.UserPrincipalName);
        var identity = new UpnEndpointIdentity(config.UserPrincipalName);

        var endpointAddress = new EndpointAddress(new Uri("https://" + serverName + ":" + config.PortNumber + "/SecurityTokenService/windows"), identity);
        var issuerMetadataAddress = new EndpointAddress(new Uri("https://" + serverName + ":" + config.PortNumber + "/SecurityTokenService/mex"), identity);

        WS2007HttpBinding binding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential)
        {
            Name = "WindowsAuthBinding",
            Namespace = "http://fully-qualified-namespcae.com/auth",
            MaxReceivedMessageSize = int.MaxValue
        };
        binding.Security.Message.EstablishSecurityContext = false;
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
        binding.Security.Message.NegotiateServiceCredential = false;

        var issuedSecurityTokenParams =
            new IssuedSecurityTokenParameters
            {
                IssuerAddress = endpointAddress,
                IssuerMetadataAddress = issuerMetadataAddress,
                DefaultMessageSecurityVersion = MessageSecurityVersion,
                KeyType = SecurityKeyType.SymmetricKey,
                TokenType = @"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1",
                IssuerBinding = binding
            };

        string additionalParam = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
                                    <xml>
                                        <trust:SecondaryParameters xmlns:trust=""http://docs.oasis-open.org/ws-sx/ws-trust/200512"">
                                          <trust:TokenType xmlns:trust=""http://docs.oasis-open.org/ws-sx/ws-trust/200512"">http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</trust:TokenType>
                                          <trust:KeyType xmlns:trust=""http://docs.oasis-open.org/ws-sx/ws-trust/200512"">http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey</trust:KeyType>
                                        </trust:SecondaryParameters>
                                        <prodName:customCode xmlns:prodName=""http://fully-qualified-namespcae.com/11""></prodName:customCode>
                                        <prodName:customCode xmlns:prodName=""http://fully-qualified-namespcae.com/11""></prodName:customCode>
                                    </xml>";

        var doc = new XmlDocument();
        doc.LoadXml(additionalParam);
        doc.GetElementsByTagName("prodName:customCode").Item(0).InnerText = customCode;
        doc.GetElementsByTagName("prodName:MsLogon").Item(0).InnerText = msLogon;
        issuedSecurityTokenParams.AdditionalRequestParameters.Add((XmlElement)doc.GetElementsByTagName("trust:SecondaryParameters").Item(0));
        issuedSecurityTokenParams.AdditionalRequestParameters.Add((XmlElement)doc.GetElementsByTagName("prodName:customCode").Item(0));
        issuedSecurityTokenParams.AdditionalRequestParameters.Add((XmlElement)doc.GetElementsByTagName("prodName:MsLogon").Item(0));
        return issuedSecurityTokenParams;
    }

Can you please give any reference, how can we migrate this to .net 6?

0

There are 0 best solutions below