Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Last active January 27, 2018 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/a127d1c9f83967658c2bbbed4693c022 to your computer and use it in GitHub Desktop.
Save Dviejopomata/a127d1c9f83967658c2bbbed4693c022 to your computer and use it in GitHub Desktop.
Add certificates to trusted root CA
using Microsoft.Extensions.CommandLineUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
namespace import_certs
{
class Program
{
static void Main(string[] args)
{
var app = new CommandLineApplication();
app.Name = "import-certs";
app.HelpOption("-?|-h|--help");
var certsDirArg = app.Option("--dir", "Directorio de los certificados", CommandOptionType.SingleValue);
app.OnExecute(() =>
{
var certsDir = certsDirArg.Value();
Console.WriteLine($"Añadiendo los certificados del directorio {certsDir}");
var certs = new List<string>(Directory.GetFiles(Path.GetFullPath(certsDir).ToString()))
.Where(file => file.EndsWith(".crt"))
.Select(file => new X509Certificate2(file))
.ToList();
using (X509Store store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadWrite | OpenFlags.IncludeArchived);
certs.ForEach(store.Add);
store.Close();
}
Console.WriteLine("Pulse alguna tecla para eliminar los certificados");
Console.ReadKey();
Console.WriteLine("Eliminando certificados");
using (X509Store store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadWrite | OpenFlags.IncludeArchived);
certs.ForEach(store.Remove);
store.Close();
}
return 0;
});
app.Execute(args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment