X509certificate2ui.Selectfromcollection is closing the application

Asked

Viewed 93 times

0

In a particular client, when accessing the function X509Certificate2UI.SelectFromCollection the application is closing without even passing the error handling.

This function opens a Windows dialog box for the user to select a digital certificate.

I am using . NET Framework 4.5, and the problem happens on a Windows 10 Pro machine, 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 function X509Certificate2UI.SelectFromCollection.

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 mistake?

1 answer

0

William, I don’t know if you solved the problem, but in my case I changed the line:

X509Certificate2Collection certificados = X509Certificate2UI.SelectFromCollection(fcollection, "Certificados Cadastrados", "Selecione seu certificado", X509SelectionFlag.SingleSelection);

for

X509Certificate2Collection certificados = X509Certificate2UI.SelectFromCollection(fcollection, "Certificados Cadastrados", "Selecione seu certificado", X509SelectionFlag.**MultiSelection**);

The screen that opens is different, allowing to select more than one certificate, but for my application, it always takes the first selection and had no problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.