On a given client, when accessing the X509Certificate2UI.SelectFromCollection
function the application is closing without even going through the error handling.
This function opens a Windows dialog box for the user to select a digital certificate.
I'm using the .NET Framework 4.5, and the problem happens on a machine with Windows 10 Pro, which has about 8 valid certificates.
I made a test application, to have the logs and find out exactly where the problem happens, and it is when using the X509Certificate2UI.SelectFromCollection
function.
static void Main(string[] args)
{
Console.WriteLine("Iniciando a aplicação para seleção de certificado");
try
{
Console.WriteLine("new X509Store");
X509Store store = new X509Store(StoreLocation.CurrentUser);
Console.WriteLine("store.Open");
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
// Obtém a coleção de certificados instalados
Console.WriteLine("store.Certificates");
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
// Excluí da coleção certificados vencidados, comparando a data corrente
Console.WriteLine("collection.Find");
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
Console.WriteLine("X509Certificate2UI.SelectFromCollection");
X509Certificate2Collection certificados = X509Certificate2UI.SelectFromCollection(fcollection, "Certificados Cadastrados", "Selecione seu certificado", X509SelectionFlag.SingleSelection);
Console.WriteLine("store.Close");
store.Close();
Console.WriteLine("certificados.Count");
if (certificados.Count == 0)
{
}
Console.WriteLine("certificados[0]");
var teste = certificados[0];
Console.WriteLine("certificados[0].Subject");
var texto = certificados[0].Subject;
}
catch (Exception ex)
{
Console.WriteLine("ex.Message");
Console.WriteLine(ex.Message);
}
Console.WriteLine("Finalizando");
Console.ReadKey();
}
Does anyone have any idea what's causing this error?