Pergunta

Estou tentando definir o nome amigável do certificado durante o processo de solicitação/aceitação do certificado. Entendo que isso é uma propriedade da Microsoft Store, em vez do certificado, e uma técnica .NET/C# pode ser usada para defini -lo.

Foi útil?

Solução 2

Então, aqui está um exemplo de linha do Commmand de como fazer isso. Você precisa de Capicom da Microsoft, que envolve o Cryptoapi.

O nome amigável é uma propriedade da loja Cert e não do certificado, portanto, este código importa um certificado para a loja Cert e define o nome amigável como faz isso.

O código leva dois parâmetros o caminho para o arquivo cert e o nome amigável que você deseja definir.

Código:-

using System;

using System.Collections.Generic;

using System.Text;

using CAPICOM;

using System.Collections;

using System.Runtime.InteropServices;


namespace CertTool

{

    class Program
    {
        const uint CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x20000;
        const int CAPICOM_PROPID_FRIENDLY_NAME = 11;
        const int CAPICOM_ENCODE_BINARY = 1;

        static private String _currStoreName = "My";
        static private String _FriendlyName = "Not Set";
        static private String _CertPath = "C:\\test.cer";
        static StoreClass _oCurrStore;
        static ExtendedPropertyClass _friendlyProp;
        static CertificateClass _certificate;
        static ExtendedProperties _extendedProp;

        static void Main(string[] args)
        {
            try
            {
                //Friendly name Argument
                if (args.Length > 0)
                {
                    _FriendlyName = args[0];
                }
                //Certpath argument
                if (args.Length > 1) 
                {
                    _CertPath = args[1];
                }
                //Set and open the Store
                _oCurrStore = new StoreClass();
                _oCurrStore.Open(
                    CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE,
                    _currStoreName,
                    CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
                    CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED);
                //Call the import certificate function
                importCert();
            }
            catch(Exception ex){
                Console.WriteLine(ex.Message);
                Console.WriteLine(args[0]);
            }
        }
        //Function import the certificate to the machine store and sets the friendly name
        static bool importCert()
        {
            try
            {
                //Create Certificate Object
                _certificate = new CertificateClass();
                //Load the certificate into the obejct from file
                _certificate.Load(_CertPath, "", CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE, CAPICOM_KEY_LOCATION.CAPICOM_LOCAL_MACHINE_KEY);
                //Create extended property Class for friendly name
                _friendlyProp = new ExtendedPropertyClass();
                _friendlyProp.PropID =  CAPICOM_PROPID.CAPICOM_PROPID_FRIENDLY_NAME;
                _friendlyProp.set_Value(CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BINARY, _FriendlyName);

                //Add extendedProp on cert object
                _extendedProp = _certificate.ExtendedProperties();
                //Set extendded prop to friendly name object
                _extendedProp.Add(_friendlyProp);
                _oCurrStore.Add(_certificate);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(_CertPath);
                return true;
            }
        }
    }
}

Outras dicas

Use x509Certificate2.FriendlyName. No entanto, você deve exportar o certificado como PFX/PKCS#12:

X509Certificate2 certificate = new X509Certificate2(...);
certificate.FriendlyName = "MyName";
File.WriteAllBytes(path, certificate.Export(X509ContentType.Pkcs12));

Ok, encontrei uma resposta para isso aqui:

Oi,

Por favor, dê uma olhada nisso para verificar se atende à sua necessidade:

Ao executar o código .NET no ambiente x64, você receberá a seguinte mensagem de erro.

"Falha -retendo a fábrica da classe com o componente com o CLSID ...."

Por exemplo, no CMS Exportar / Importar Lado do servidor .NET Código = "O exportSITECONTENTINCREMENTAL (...) falhou-retirando a fábrica da classe COM para o componente com CLSID {CA0752B3-021C-4F99-82E3-2C0F19C5E953} falhou devido ao seguinte erro: 80040154."

GAMBIARRA:

A possível solução alternativa é modificar a plataforma do seu projeto de 'qualquer CPU' para 'x86' (nas propriedades do projeto, o alvo da construção/plataforma)

CAUSA RAIZ

A interop VSS é uma montagem gerenciada usando a estrutura de 32 bits e a DLL contém um objeto COM de 32 bits. Se você executar esse com dll em ambiente de 64 bits, receberá a mensagem de erro.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top