/// <summary> /// Replicates generated certificate /// </summary> /// <param name="userName">Certificate file name</param> /// <param name="password">Password for .pvk file</param> private void Replicate(string userName, string password) { /// try-catch necessary if either the speficied file doesn't exist or password is incorrect try { X509Certificate2 certificate; if (password == "") { certificate = new X509Certificate2(userName + ".cer"); } else { certificate = new X509Certificate2(userName + ".cer", password); } NetTcpBinding binding = new NetTcpBinding(); InitializeWindowsAuthentication(binding); EndpointAddress address = new EndpointAddress(new Uri(ConfigurationSettings.AppSettings.Get("BackUp"))); using (WCFBackupClient proxy = new WCFBackupClient(binding, address)) { message = String.Format("Client {0} with certificate[Subject {1}] successfully replicated.", userName, certificate.Subject); evntType = EventLogEntryType.SuccessAudit; EventLogManager.WriteEntryCMS(message, evntType, Convert.ToInt32(IDType.ReplicateSuccess)); proxy.ReplicateCertificate(certificate.Subject + ", thumbprint: " + certificate.Thumbprint); } } catch (Exception e) { message = String.Format("Error with replicating client {0} certificate.Error: {1}", userName, e.Message); evntType = EventLogEntryType.SuccessAudit; EventLogManager.WriteEntryCMS(message, evntType, Convert.ToInt32(IDType.ReplicateFailure)); Console.WriteLine("Error while trying to replicate certificate {0}. ERROR = {1}", userName, e.Message); } }
public void createRootCertificate(string root) { if (File.Exists(root + ".cer")) { Console.WriteLine("Self-signed certificate <{0}> already exists", root); return; } Process p = new Process(); string path = (AppDomain.CurrentDomain.BaseDirectory + @"\makecert.exe"); string arguments = string.Format("-n \"CN = {0}\" -r -sv {0}.pvk {0}.cer", root); ProcessStartInfo info = new ProcessStartInfo(path, arguments); p.StartInfo = info; try { p.Start(); } catch (Exception e) { message = String.Format("Root certificate {0} cannot be generated.Error: {1}", root, e.Message); evntType = EventLogEntryType.FailureAudit; EventLogManager.WriteEntryCMS(message, evntType, Convert.ToInt32(IDType.GenerateFailure)); return; } message = String.Format("Root certificate {0} generated.", root); evntType = EventLogEntryType.SuccessAudit; EventLogManager.WriteEntryCMS(message, evntType, Convert.ToInt32(IDType.GenerateSuccess)); p.WaitForExit(); p.Dispose(); Console.WriteLine("Created new self-signed certificate"); /// try-catch necessary if either the speficied file doesn't exist or password is incorrect try { X509Certificate2 certificate = new X509Certificate2(root + ".cer"); NetTcpBinding binding = new NetTcpBinding(); InitializeWindowsAuthentication(binding); EndpointAddress address = new EndpointAddress(new Uri(ConfigurationSettings.AppSettings.Get("BackUp"))); using (WCFBackupClient proxy = new WCFBackupClient(binding, address)) { message = String.Format("Root certificate {0} successfully replicated.", root); evntType = EventLogEntryType.SuccessAudit; EventLogManager.WriteEntryCMS(message, evntType, Convert.ToInt32(IDType.ReplicateSuccess)); proxy.ReplicateCertificate(certificate.Subject + ", thumbprint: " + certificate.Thumbprint); } } catch (Exception e) { message = String.Format("Root certificate {0} failed to replicate.Error: {1}", root, e.Message); evntType = EventLogEntryType.FailureAudit; EventLogManager.WriteEntryCMS(message, evntType, Convert.ToInt32(IDType.ReplicateFailure)); Console.WriteLine("Error while trying to replicate certificate {0}. ERROR = {1}", root, e.Message); } }