Last active
January 27, 2018 11:03
-
-
Save Dviejopomata/a127d1c9f83967658c2bbbed4693c022 to your computer and use it in GitHub Desktop.
Add certificates to trusted root CA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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