以下のNew-SelfSignedCertificate
コマンドに匹敵する自己署名証明書を作成する簡単な方法があるかどうか不思議です(他のプロバイダもOKです)。私はP/Invokeのない.NETライブラリ、またはBouncy Castleなどの外部ライブラリを使用するか、アプリケーションからPowerShellを呼び出さないでください。外部ライブラリのない自己署名証明書を生成する
New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation $certificateStore -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $certificateNotAfter
私は最も簡単な選択肢はPowerShellを呼び出すか、これは外部の施設ずになんとかしない場合には、そのような弾む城としてNugetライブラリを用いることであろうと仮定しますか?証明書を構築する方法を十分に知っていれば、バイト配列テンプレートなどを作成し、X509Certificate2
コンストラクタでそれを使用することが可能なように感じます。それはすぐに明らかになった平野.NETでこれを行うための良い方法はありません。
は、1つが
public X509Certificate2 GenerateCertificate(string fileName, string password, string subjectName, StoreName storeName, DateTime endDate, DateTime notAfter, string provider = "Microsoft Enhanced RSA and AES Cryptographic Provider")
{
//Could provider be taken from https://stackoverflow.com/questions/43474902/generate-self-signed-rsa-2048-sha-256-certificate-pfx-file-using-openssl?
var newCertificate = new X509Certificate2(fileName, password, X509KeyStorageFlags.Exportable);
/*
# The following creates a self-signed certificate with one year of running time.
$currentDate = Get-Date
$certificateEndDate = $currentDate.AddYears(1)
$certificateNotAfter = $certificateEndDate.AddYears(1)
$certificateName = "https://www.test.com/test"
$certificateStore = "Cert:\LocalMachine\My"
New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation $certificateStore -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $certificateNotAfter
*/
}
<編集する必要があります表示されます。私が見つけた
さらにいくつかのオプション:
ブログ記事Creating Authority-Signed and Self-Signed Certificates in .NETスティーブSyfuhsと別のことでSOモノの拡張、Mono.Security won't set multiple KeyUsagesを使用して投稿してください。既に議論されている選択肢とは別に、これは「このようなもの」です。
「PowerShellを呼び出さずに」PowerShellコードを提供しています。ポイントは? – Crypt32
最後に達成したいことは? PSソリューションをお探しですか? –
'System.ServiceModel'の内部に[SelfSignedCertificate](https://referencesource.microsoft.com/#System.ServiceModel/System/ServiceModel/Channels/SelfSignedCertificate.cs,c4320314fb74ebcc)クラスが隠れているようです。しかし、それは内部的なものなので、リフレクションやコードを複製する必要がありますが、その時点でP/Invokingを再度実行します。その存在によって、私は.NETでより直接的に作成するための簡単なアプローチはないと推測します。 –