質問
証明書の要求/受け入れプロセス中に証明書のフレンドリ名を設定しようとしています。これは証明書ではなくMicrosoftストアのプロパティであり、設定にどのような.net / c#テクニックが使用されるのか疑問に思います。
解決 2
そのため、これを行う方法のコマンドラインの例を次に示します。 CryptoAPIをラップするMicrosoftのCAPICOMが必要です。
フレンドリ名は証明書ではなく証明書ストアのプロパティであるため、このコードは証明書ストアに証明書をインポートし、フレンドリ名を設定します。
このコードは、証明書ファイルへのパスと設定するフレンドリ名の2つのパラメーターを取ります。
コード:-
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;
}
}
}
}
他のヒント
X509Certificate2.FriendlyNameを使用します。ただし、証明書をPFX / PKCS#12としてエクスポートする必要があります。
X509Certificate2 certificate = new X509Certificate2(...);
certificate.FriendlyName = "MyName";
File.WriteAllBytes(path, certificate.Export(X509ContentType.Pkcs12));
OK、ここでその答えを見つけました:
こんにちは、
これを見て、ニーズに合っているかどうかを確認してください:
X64環境で.netコードを実行すると、次のエラーメッセージが表示されます。
"失敗-CLSIDを持つコンポーネントのCOMクラスファクトリを取得しています...."
E.g。 CMSエクスポート/インポートサーバー側で.net code = " ExportSiteContentIncremental(...)Failed-CLSID {CA0752B3-021C-4F99-82E3-2C0F19C5E953}のコンポーネントのCOMクラスファクトリを取得できませんでしたエラー:80040154。"
回避策:
可能な回避策は、プロジェクトのプラットフォームを「任意のCPU」から「X86」に変更することです(プロジェクトのプロパティ、ビルド/プラットフォームのターゲットで)
根本原因
VSS Interopは32ビットフレームワークを使用するマネージアセンブリであり、dllには32ビットCOMオブジェクトが含まれています。このCOM dllを64ビット環境で実行すると、エラーメッセージが表示されます。