Exemplo n.º 1
0
        public WCFClient(NetTcpBinding binding, EndpointAddress address, string clientCertificateName, ICMSCommunication proxy)
            : base(binding, address)
        {
            string cltCertCN = Formatter.ParseName(WindowsIdentity.GetCurrent().Name);

            this.Credentials.ServiceCertificate.Authentication.CertificateValidationMode  = System.ServiceModel.Security.X509CertificateValidationMode.Custom;
            this.Credentials.ServiceCertificate.Authentication.CustomCertificateValidator = new ClientCostumValidator(proxy);
            this.Credentials.ServiceCertificate.Authentication.RevocationMode             = X509RevocationMode.NoCheck;

            /// Set appropriate client's certificate on the channel. Use CertManager class to obtain the certificate based on the "cltCertCN"
            this.Credentials.ClientCertificate.Certificate = ClientGet.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, clientCertificateName);

            factory = this.CreateChannel();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.ReadKey();

            NetTcpBinding     binding2 = new NetTcpBinding();
            ICMSCommunication proxy2   = new ChannelFactory <ICMSCommunication>(binding2, new EndpointAddress("net.tcp://localhost:1324/CMS")).CreateChannel();

            string tempCertificate = WindowsIdentity.GetCurrent().Name;

            string[] parse = tempCertificate.Split('\\');

            string clientCertificateName = parse[1];

            bool temp = false;

            int num = proxy2.NumOfCertificates(clientCertificateName);

            X509Certificate2 clientCertificate = ClientGet.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, clientCertificateName);

            if (clientCertificate != null)
            {
                if (!proxy2.CheckValidation(clientCertificate))
                {
                    clientCertificate = null;
                }
            }

            if (clientCertificate == null)
            {
                clientCertificate = ClientGet.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, clientCertificateName + "New" + num);

                if (clientCertificate != null)
                {
                    if (!proxy2.CheckValidation(clientCertificate))
                    {
                        clientCertificate = null;
                    }
                }
            }

            if (clientCertificate == null)
            {
                Console.WriteLine("Enter new certificate password: "******"Sertifikat postoji i validan je.");
                }
                else
                {
                    Console.WriteLine("Sertifikat postoji i NIJE validan.");
                }
            }

            NetTcpBinding binding = new NetTcpBinding();

            binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;

            X509Certificate2 srvCert = ClientGet.GetCertificateFromTrustedPeople(StoreName.TrustedPeople, StoreLocation.LocalMachine);

            EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:1234/Server"), new X509CertificateEndpointIdentity(srvCert));

            Thread.Sleep(3000);
            using (WCFClient proxy = new WCFClient(binding, address, clientCertificateName, proxy2))
            {
                Random r = new Random();
                Console.WriteLine("\n*********	Menu	**********");
                Console.WriteLine("* 1. Withdrawal certificate.	 *");
                Console.WriteLine("* 2. Establish communication.    *");
                Console.WriteLine("* 3. Exit                        *");
                Console.WriteLine("**********************************");
                Console.WriteLine("Select option: ");
                int select = int.Parse(Console.ReadLine());

                if (select == 1)
                {
                    Console.WriteLine("Enter new certificate password: "******"ERROR: Selected option error!");
                }
            }

            Console.WriteLine("INFO: Enter key to exit.");
            Console.ReadKey();
        }