コード例 #1
0
        // bindingName = nome do binding definido no arquivo de configuração (EX: PingServiceBinding);
        // messageHeaderNamespace = dado do namespace remoto a ser definido no header da mensagem;
        public static BasicHttpBinding GetBindingCCEE(ParametrosConexaoCCEE param)
        {
            try
            {
                X509Certificate2Collection certificates = GetCertificadosCCEE(param);
                if (certificates == null)
                {
                    throw new Exception("ERRO NA PREPARAÇÃO DOS CERTIFICADOS");
                }

                // criar conexão ssl e meio de transporte (https)
                BasicHttpBinding binding = new BasicHttpBinding();
                binding.Name          = param.BindingName;
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
                binding.SendTimeout   = TimeSpan.FromSeconds(120);
                binding.Namespace     = param.MessageHeaderNamespace;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
                binding.Security.Message.ClientCredentialType   = BasicHttpMessageCredentialType.UserName;

                return(binding);
            }
            catch
            {
                return(null);
            }
        }
コード例 #2
0
        // prepara os certificados a serem utilizados na conexão com a CCEE:
        // define local, nome e senha onde ficam os certificados para serem enviados;
        public static X509Certificate2Collection GetCertificadosCCEE(ParametrosConexaoCCEE param)
        {
            try
            {
                var clientCertificate         = param.CertCliente;
                var clientCertificatePassword = param.CertClientePwd;
                var serverCertificate         = param.CertServer;
                var serverCertificatePassword = param.CertServerPwd;

                //var filePath = @"C:\Users\davelar\source\repos\AccessCCEE\AccessCCEE\Services\ChaveEnviadaParaCCEE.p12";
                //var serverCertificatePath = @"C:\Users\davelar\source\repos\AccessCCEE\AccessCCEE\Services\servicoscceeorgbr.crt";

                // montar os certificados;
                var certificates = new X509Certificate2Collection(new X509Certificate2(clientCertificate, clientCertificatePassword, X509KeyStorageFlags.PersistKeySet));
                certificates.Add(new X509Certificate2(serverCertificate, serverCertificatePassword, X509KeyStorageFlags.PersistKeySet));
                return(certificates);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }