protected override Binding CreateBinding <TChannel>(SoapChannelCreationContext context)
        {
            var binding = null as Binding;

            if (context.Properties.TryGetValue("handler", out var handler))
            {
                binding = CreateFederationBinding(handler as SecurityTokenHandler, context);
            }
            else if (context.Properties.TryGetValue("userName", out _) && context.Properties.TryGetValue("password", out _))
            {
                binding = CreateBinding(MessageCredentialType.UserName, context);
            }
            else if (context.Properties.TryGetValue("certificate", out _))
            {
                binding = CreateBinding(MessageCredentialType.Certificate, context);
            }

            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            binding = binding.WithoutTransportSecurity();

            if (context.Properties.TryGetValue("settings", out var writerSettings))
            {
                binding = binding.WithSolidHttpTransport(TestingServer, writerSettings: writerSettings as XmlWriterSettings);
            }
            return(binding);
        }
        private Binding CreateBinding(MessageCredentialType credentialType, SoapChannelCreationContext context)
        {
            var binding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential);

            binding.Security.Message.EstablishSecurityContext = false;
            binding.Security.Message.ClientCredentialType     = credentialType;

            if (context.Properties.TryGetValue("securityAlgorithmSuite", out var value) && value is SecurityAlgorithmSuite securityAlgorithmSuite)
            {
                binding.Security.Message.AlgorithmSuite = securityAlgorithmSuite;
            }

            return(binding);
        }
        private Binding CreateFederationBinding(SecurityTokenHandler handler, SoapChannelCreationContext context)
        {
            var binding = new WsTrustIssuedTokenBinding();

            binding.KeyType = System.IdentityModel.Tokens.SecurityKeyType.BearerKey;
            binding.Security.Message.EstablishSecurityContext = false;
            //binding.Security.Message.IssuedTokenType = handler.GetTokenTypeIdentifiers().FirstOrDefault();

            if (context.Properties.TryGetValue("securityAlgorithmSuite", out var value) && value is SecurityAlgorithmSuite securityAlgorithmSuite)
            {
                binding.Security.Message.AlgorithmSuite = securityAlgorithmSuite;
            }

            return(binding);
        }
        public IWsTrustChannelContract CreateWsTrust13UserNameClient(string userName, string password, string appliesTo = "urn:tests", string issuer = "urn:test:issuer", SecurityAlgorithmSuite securityAlgorithmSuite = null)
        {
            var properties = new Dictionary <string, object>
            {
                { "userName", userName },
                { "password", password }
            };

            if (securityAlgorithmSuite != null)
            {
                properties.Add("securityAlgorithmSuite", securityAlgorithmSuite);
            }
            var context = SoapChannelCreationContext.Create <IWsTrustChannelContract>(path: "trust/13", MessageVersion.Default, reusable: false, properties: properties);
            var channel = CreateChannel <IWsTrustChannelContract>(context);

            return(channel);
        }
        public IWsTrustChannelContract CreateWsTrust13CertificateClient(X509Certificate2 certificate, XmlWriterSettings writerSettings = null, SecurityAlgorithmSuite securityAlgorithmSuite = null)
        {
            var properties = new Dictionary <string, object>
            {
                { "certificate", certificate }
            };

            if (writerSettings != null)
            {
                properties.Add("settings", writerSettings);
            }
            if (securityAlgorithmSuite != null)
            {
                properties.Add("securityAlgorithmSuite", securityAlgorithmSuite);
            }

            var context = SoapChannelCreationContext.Create <IWsTrustChannelContract>(path: "trust/13", MessageVersion.Default, reusable: false, properties: properties);
            var channel = CreateChannel <IWsTrustChannelContract>(context);

            return(channel);
        }
        public IWsTrustChannelContract CreateWsTrust13IssuedTokenClient(string subject, string clientTokenType = Saml2TokenType, string appliesTo = "urn:tests", string issuer = "urn:test:issuer", SecurityAlgorithmSuite securityAlgorithmSuite = null)
        {
            var identity = CreateIdentity(subject);
            var token    = CreateSecurityToken(identity, clientTokenType, appliesTo, issuer);

            if (!_handlers.TryGetValue(clientTokenType, out var handler))
            {
                throw new Exception($"Security handler not found for '{clientTokenType}'");
            }
            var properties = new Dictionary <string, object>
            {
                { "token", token },
                { "handler", handler }
            };

            if (securityAlgorithmSuite != null)
            {
                properties.Add("securityAlgorithmSuite", securityAlgorithmSuite);
            }
            var context = SoapChannelCreationContext.Create <IWsTrustChannelContract>(path: "trust/13", MessageVersion.Default, reusable: false, properties: properties);
            var channel = CreateChannel <IWsTrustChannelContract>(context);

            return(channel);
        }
 protected override ICommunicationObject CreateChannel <TChannel>(ChannelFactory <TChannel> factory, SoapChannelCreationContext context)
 {
     if (factory is WsTrustChannelFactory wsTrust)
     {
         if (context.Properties.TryGetValue("userName", out var userName) && context.Properties.TryGetValue("password", out var password))
         {
             wsTrust.Credentials.UserName.UserName = userName as string;
             wsTrust.Credentials.UserName.Password = password as string;
             return(wsTrust.CreateChannel() as ICommunicationObject);
         }
         else if (context.Properties.TryGetValue("certificate", out var certificate))
         {
             wsTrust.Credentials.ClientCertificate.Certificate = certificate as X509Certificate2;
             return(wsTrust.CreateChannel() as ICommunicationObject);
         }
         else if (context.Properties.TryGetValue("token", out var token))
         {
             return(wsTrust.CreateChannelWithIssuedToken(token as SecurityToken) as ICommunicationObject);
         }
     }
     return(base.CreateChannel <TChannel>(factory, context));
 }
        protected override ChannelFactory <TChannel> CreateChannelFactory <TChannel>(Binding binding, EndpointAddress endpointAddress, SoapChannelCreationContext context)
        {
            var factory = new WsTrustChannelFactory(binding, endpointAddress);

            factory.TrustVersion = WsTrustVersion.Trust13;
            if (context.Properties.TryGetValue("handler", out var handler))
            {
                var other = factory.SecurityTokenHandlers.FirstOrDefault(h => h.GetType() == handler.GetType());
                if (other != null)
                {
                    factory.SecurityTokenHandlers.Remove(other);
                }
                factory.SecurityTokenHandlers.Add(handler as SecurityTokenHandler);
            }
            return(factory as ChannelFactory <TChannel>);
        }
 protected override EndpointAddress CreateEndpointAddress <TChannel>(Uri url, SoapChannelCreationContext context)
 => new EndpointAddress(url);