Question

J'essaie de définir le nom convivial du certificat pendant le processus de demande / acceptation de certificat. Je comprends qu’il s’agit d’une propriété de la boutique Microsoft plutôt que du certificat et que je me demande quelle technique .net / c # pourrait être utilisée pour le définir.

Était-ce utile?

La solution 2

Voici donc un exemple de ligne de commande expliquant comment procéder. Vous avez besoin de CAPICOM de Microsoft qui enveloppe CryptoAPI.

Le nom convivial est une propriété du magasin de certificats plutôt que du certificat. Par conséquent, ce code importe un certificat dans le magasin de certificats et définit le nom convivial de la même manière.

Le code prend deux paramètres, le chemin d'accès au fichier de certification et le nom convivial que vous souhaitez définir.

Code: -

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;
            }
        }
    }
}

Autres conseils

Utilisez X509Certificate2.FriendlyName. Cependant, vous devez exporter le certificat en tant que PFX / PKCS # 12:

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

Ok, vous avez trouvé une réponse à cette question:

Bonjour,

Veuillez regarder ceci pour vérifier si cela répond à vos besoins:

Lorsque vous exécutez le code .net dans un environnement X64, vous obtenez le message d'erreur suivant.

" Echec - Récupération de la fabrique de classe COM pour le composant avec CLSID .... "

E.g. code .net côté serveur d'exportation / importation CMS = " ExportSiteContentIncremental (...) Failed --L'extraction de la fabrique de classes COM pour le composant avec CLSID {CA0752B3-021C-4F99-82E3-2C0F19C5E953} a échoué erreur: 80040154. "

Contournement:

La solution de contournement possible consiste à modifier la plate-forme de votre projet de "Tout processeur" à "X86" (dans les propriétés du projet, la cible de Build / Platform)

ROOTCAUSE

VSS Interop est un assemblage géré utilisant une structure 32 bits et la dll contient un objet COM 32 bits. Si vous exécutez cette dll COM dans un environnement 64 bits, vous obtiendrez le message d'erreur.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top