自己署名証明書(Httplistener with https supportのアドバイス)を使用してテストC#Https Listenerをセットアップしました。HttpsをサポートするLocalhost HttpListener - 一定期間後に動作しなくなる
(ローカルシステムでWindowsサービスとして実行される)サービスとは、最初に(起動後)がうまく動作します。一定の期間(〜1.5時間)後 はエッジ/ IEの訴えでの作業を停止HTTPSエンドポイントへの呼び出し:
は、このページにしっかり を接続できないサイトが古くなったり、安全でない使用していますので、これがあるかもしれませんTLSセキュリティ設定。これが起こっている場合は、ウェブサイトの所有者に連絡してみてください。このサイトは 接続がリセットされたに到達することはできません
次のように
Chromeが文句を言います。 ERR_CONNECTION_RESET
この場合、証明書ストアを確認すると、証明書(ルートと私の店舗)がまだ存在することが示されます。
をチェック
のsslcertのnetshのhttpショーはまた、ポートへの証明書の登録が場所にまだあることを示しています。
アプリケーションを再起動すると(C#httpリスナーがリッスンするポートへの証明書の再作成、再インストール、再バインド)役立ちますが、しばらくの間(〜1.5 ..2時間?)。
私は、リスナースレッドがまだ安全でないポートの要求が依然として機能していることを知っています。
何かがその間に起こると私は理解できないものを...
コード:
すべてが自己署名証明書の生成から始まる:
// create DN for subject and issuer
var dn = new CX500DistinguishedName();
dn.Encode("CN=localhost");
// create a new private key for the certificate
var privateKey = new CX509PrivateKey
{
ProviderName = "Microsoft Base Cryptographic Provider v1.0",
MachineContext = false,
Length = 2048,
KeySpec = X509KeySpec.XCN_AT_SIGNATURE,
KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_SIGNING_FLAG,
FriendlyName = "Application Testing Key",
ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_FLAG
};
privateKey.Create();
var hashobj = new CObjectId();
hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY,
AlgorithmFlags.AlgorithmFlagsNone, "SHA256");
// Create the self signing request
var certificateRequest = new CX509CertificateRequestCertificate();
certificateRequest.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, String.Empty);
certificateRequest.Subject = dn;
certificateRequest.Issuer = dn; // the issuer and the subject are the same
certificateRequest.NotBefore = DateTime.UtcNow.AddDays(-1);
certificateRequest.NotAfter = DateTime.UtcNow.AddYears(10);
certificateRequest.HashAlgorithm = hashobj;
// Set up the Subject Alternative Names extension.
var nameslist = new CAlternativeNames();
var alternativeName = new CAlternativeName();
alternativeName.InitializeFromString(AlternativeNameType.XCN_CERT_ALT_NAME_DNS_NAME, "localhost");
nameslist.Add(alternativeName);
var subjectAlternativeNamesExtension = new CX509ExtensionAlternativeNames();
subjectAlternativeNamesExtension.InitializeEncode(nameslist);
certificateRequest.X509Extensions.Add((CX509Extension)subjectAlternativeNamesExtension);
var skiExtension = new CX509ExtensionSubjectKeyIdentifier();
skiExtension.InitializeEncode(EncodingType.XCN_CRYPT_STRING_BASE64, Convert.ToBase64String(StringToByteArray(certSKI)));
certificateRequest.X509Extensions.Add((CX509Extension)skiExtension);
certificateRequest.Encode();
// Do the final enrollment process
var enroll = new CX509Enrollment();
enroll.InitializeFromRequest(certificateRequest); // load the certificate
enroll.CertificateFriendlyName = "Application Testing Cert";
var csr = enroll.CreateRequest(); // Output the request in base64
var pwd = Guid.NewGuid().ToString();
enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate, csr, EncodingType.XCN_CRYPT_STRING_BASE64, pwd); // and install it back as the response
var base64encoded = enroll.CreatePFX(pwd, PFXExportOptions.PFXExportChainWithRoot);
// instantiate the target class with the PKCS#12 data
return new X509Certificate2(Convert.FromBase64String(base64encoded), pwd);
ローカルホストのルートとプライベートストアに新しく生成された証明書を追加します。
InstallCertificateToCertStore(cert, new X509Store(StoreName.Root, StoreLocation.LocalMachine));
InstallCertificateToCertStore(cert, new X509Store(StoreName.My, StoreLocation.LocalMachine));
HTTPリスナーがリッスンするポートを新しく作成してインストール証明書を登録することにより、続い:
netsh.exe http add sslcert ipport=0.0.0.0:{port} certhash={certThumbprint} appid={appid_guid}
さらにいくつかの大規模な検索後に理由があることもできるように思えます私は秘密鍵を保持しません:https://support.microsoft.com/en-us/help/950090/installing-a-pfx-file-using-x509certificate-from-a-standard--net-appli Testing今。 –