Example #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                // check that the certificate exists (doubling clicking on the file in explorer will bring up the certificate wizard).
                if (SecurityUtils.InitializeCertificate(StoreName.My, StoreLocation.CurrentUser, "My Server Name") == null)
                {
                    MessageBox.Show("Please import the 'My Server Certificate.pfx' file into the CurrentUser/Personal certificate store (password is 'password')");
                    return;
                }

                // create a self-hosted service.
                using (ServiceHost serviceHost = new ServiceHost(typeof(MyService)))
                {
                    serviceHost.Open();
                    Application.Run(new ServerForm());
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Example #2
0
        /// <summary>
        /// Creates a new client instance which can connect to the specified server.
        /// </summary>
        /// <param name="url">The URL for the server.</param>
        /// <param name="serverCertificate">The certificate used by the server.</param>
        /// <returns>The new client object.</returns>
        public static MyClient Create(string url, X509Certificate2 serverCertificate)
        {
            // Look up client certificate.
            X509Certificate2 clientCertificate = SecurityUtils.InitializeCertificate(
                StoreName.My,
                StoreLocation.CurrentUser,
                "My Client Name");

            // The private key is what the client uses to prove that it is the legimate holder
            // of the certificate. It is stored in a location that can only be accessed by the
            // current user or the adminstrator. We cannot continue if the private key is missing.
            if (clientCertificate == null || !clientCertificate.HasPrivateKey)
            {
                throw new StatusCodeException(
                          StatusCodes.BadConfigurationError,
                          "Cannot find client certificate or the private key is missing.");
            }

            // The endpoint description stores the information required to connect to the server.
            // This includes the security settings which have to be used to initialize the WCF binding.
            // This structure is what is returned by a UA discovery server.
            EndpointDescription endpoint = CreateEndpointDescription(url, serverCertificate);

            // Initialize the binding that is used to connect.
            // The binding configurations are specified in the app.config file.
            Binding binding = CreateSessionBinding(endpoint);

            // The stack needs some way to verify that it is connecting to the correct server.
            // In this case we assume that the client has verified the server certificate and
            // tell the stack to check for it when connecting.
            EndpointIdentity serverIdentity = EndpointIdentity.CreateX509CertificateIdentity(serverCertificate);

            // Associate the endpoint url with the server certificate.
            EndpointAddress address = new EndpointAddress(new Uri(endpoint.EndpointUrl), serverIdentity);

            // Instantiate the channel (does not actually connect).
            MyClient channel = new MyClient(binding, address);

            channel.m_clientCertificate = clientCertificate;

            // Add the client certificate into the behavoirs.
            ClientCredentials credentials = (ClientCredentials)channel.ChannelFactory.Endpoint.Behaviors[typeof(ClientCredentials)];

            if (clientCertificate != null)
            {
                credentials.ClientCertificate.Certificate = clientCertificate;
            }

            return(channel);
        }