私はこのコードを持っています:
string certificateFilePath = @"C:\Users\Administrator\Documents\Certificate.pfx";
string certificateFilePassword = "Some Password Here";
X509Certificate clientCertificate = new X509Certificate(certificateFilePath, certificateFilePassword);
TcpClient client = new TcpClient(host, port);
SslStream stream = new SslStream(client.GetStream(), false, (sender, certificate, chain, errors) => true);
X509CertificateCollection clientCertificates = new X509CertificateCollection {clientCertificate};
stream.AuthenticateAsClient(host, clientCertificates, SslProtocols.Tls, false);
コンソール アプリケーションでコードを実行すると、すべて正常に動作し、正しい証明書オブジェクトが返され、stream.IsAuthenticated
含まれています。stream.IsMutuallyAuthenticated
true
stream.LocalCertificate
ただし、まったく同じコードを aWindows Service (as LOCAL SYSTEM user)
で実行すると、 return 、return 、およびreturn がstream.IsAuthenticated
返されます。true
stream.IsMutuallyAuthenticated
false
stream.LocalCertificate
null
これは、両方のシナリオで、最初の行が実行された後にclientCertificate
、正しい認証データが読み込まれ、証明書の正しい情報が含まれているときに発生Subject
しますIssuer
。
また、このコードを使用して SslStream に強制的に証明書を選択させようとしました:
string certificateFilePath = @"C:\Users\Administrator\Documents\Certificate.pfx";
string certificateFilePassword = "Some Password Here";
X509Certificate clientCertificate = new X509Certificate(certificateFilePath, certificateFilePassword);
TcpClient client = new TcpClient(host, port);
SslStream stream = new SslStream(client.GetStream(), false, (sender, certificate, chain, errors) => true, (sender, host, certificates, certificate, issuers) => clientCertificate);
X509CertificateCollection clientCertificates = new X509CertificateCollection {clientCertificate};
stream.AuthenticateAsClient(host, clientCertificates, SslProtocols.Tls, false);
ただし、コードはまだ機能せず、stream.IsMutuallyAuthenticated
戻り値false
とstream.LocalCertificate
戻り値が返されますnull
。
私はこれを数日間調査してきましたが、理解できません。どんな助けでも大歓迎です。
編集: WinHttpCertCfgツールで
証明書を試した後、同様の質問とは異なり、下の図に示すように、LOCAL SYSTEM アカウントは既にターゲット証明書の秘密鍵にアクセスできることがわかりました。
したがって、問題はまだ解決されていません。