Fetch the certificate from the client, not from the server

Asked

Viewed 272 times

6

I need to get the certificate, and I would like you to search the client, not the server, I try to do this way:

 public static X509Certificate2 EscolherCertificado(string serial)
    {
        var store = new X509Store("MY", StoreLocation.CurrentUser);

        var Key = new RSACryptoServiceProvider();
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection collection = store.Certificates;
        X509Certificate2Collection fcollection = collection.Find(X509FindType.FindBySerialNumber, serial, false);
        if (fcollection.Count == 1)
        {
            return fcollection[0];
        }
        else { cod = "00000"; msgm = "Certificado não identificado, reinicie o certificado e tente novamente"; return null; }

    }

But when I post to the server it doesn’t work. It has some way I can do it ?

How can I do, because in this case is returning error

Object Reference not set to an instance of an Object.

Because it doesn’t even appear for the user to enter the certificate password, I need to change something in the file signature too ? Thank you.

EDIT So far I have not succeeded, I need the application to get the certificate installed on the client. And appear the password to enter the client.

EDIT I tried to change to var store = new X509Store("MY", StoreLocation.LocalMachine); but also does not search for the certificate in the customer.

EDIT I found this link on the internet, however I could not work, it tells how to catch the client’s user. What I may be doing wrong. I changed the code to:

public static X509Certificate2 EscolherCertificado(string serial)
        {
        X509Store userCaStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        try
        {
            userCaStore.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certificatesInStore = userCaStore.Certificates;
            X509Certificate2Collection findResult = certificatesInStore.Find(X509FindType.FindBySerialNumber, serial, true);
            X509Certificate2 clientCertificate = null;
            if (findResult.Count == 1)
            {
                clientCertificate = findResult[0];
            }
            else
            {
                throw new Exception("Unable to locate the correct client certificate.");
            }
            cod = "0000"; msgm = clientCertificate.ToString(); return clientCertificate;
        }
        catch
        {
            throw;
        }
        finally
        {
            userCaStore.Close();
        }

EDIT There is some way to get javascript so I need to get the certificate that is in the client to sign, while the application runs on the server. I need to get the certificate according to the serial that’s registered at the bank, and sign the file xml.

Remembering I’ve tried switching to LocalMachine, but also did not resolve.

  X509Store userCaStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  • If you want to search on the client then use some application/module that runs on the client (client side) and not on the server (server side).

  • I need everything on the server, and just search on the client.

  • 1

    Mari, you there again hahaha...

  • What an exact certificate you’re looking for?

  • @gabrielfalieri I again, as smp hahahahahaha, I’m trying for A3, smartcard

  • Remember that the A3 certificate it has extra protection, so the private key is inaccessible except for the hardware... what you can get is the public

  • What would be the solution, does it have any ? rs, the certificate will stay on the client, the application needs to select the certificate, and sign a file that will be on the client’s machine.

  • The solution will be to build a WCF service where a method will be created that will perform the operation that needs to be performed, and within this method you will use your code to fetch the certificate. And this service will be installed on the client machine so you can get the certificate.

Show 4 more comments
No answers

Browser other questions tagged

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