- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这个代码:
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
包含正确的证书对象。
然而,当在 Windows 服务(作为 LOCAL SYSTEM 用户)
中运行完全相同的代码时,尽管 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 工具尝试证书后,结果发现与 similar question(s) 不同, LOCAL SYSTEM 帐户已经可以访问目标证书的私钥,如下图所示: 因此问题仍然没有解决。
最佳答案
我终于在使用 X509 类时使代码工作。
这是对我有用的代码:
string host = "The Host";
int port = 777;
string certificateFilePath = @"C:\Users\Administrator\Documents\Certificate.pfx";
string certificateFilePassword = "Some Password Here";
X509Certificate clientCertificate = new X509Certificate(certificateFilePath, certificateFilePassword);
X509Certificate2 clientCertificate2 = new X509Certificate2(clientCertificate); //<== Create a X509Certificate2 object from the X509Certificate which was loaded from the file. The clientCertificate2 loads the proper data
TcpClient client = new TcpClient(host, port);
SslStream stream = new SslStream(client.GetStream(), false, (sender, certificate, chain, errors) => true);
X509CertificateCollection clientCertificates = new X509CertificateCollection { clientCertificate2 }; //<== Using the clientCertificate2 which has loaded the proper data instead of the clientCertificate object
stream.AuthenticateAsClient(host, clientCertificates, SslProtocols.Tls, false);
这样我的代码就可以从系统中找到正确的 X509Store、证书和私钥。
这是我的经验之谈。不过,我在 MSDN 上找不到关于为什么它应该像这样的明确解释。
关于c# - SslStream 身份验证在本地系统帐户下失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015950/
当远程客户端未发送任何内容时,我遇到了 SSLStream 返回一些数据的问题。当服务器正在监听新命令时,我遇到了这个问题。如果服务器没有收到新的请求,ReadMessage() 函数应该捕获一个 I
我最近在将套接字通信转换为使用 System.Net.Security.SslStream 而不是 NetworkStream 时遇到了一个问题。此转换针对 .Net Framework 4.8 项目
我正在尝试使用 SslStream 和 TLS 1.2 协议(protocol)与远程服务器建立 TCP 连接。代码如下: _tcpClient.Connect(endPoint); var cert
我正在尝试使用 TLS (openssl) 实现客户端-服务器应用程序。我按照 rust doc 中给出的示例作为我的代码结构:example 服务器代码 fn handle_client(mut s
当我尝试从 SslStream 函数读取时,如果我没有设置连接超时,Read() 永远不会结束,但如果我设置了连接超时异常。这家伙有同样的问题http://msdn.microsoft.com/en-
我正在尝试通过 C# .Net 应用程序中的 SslStream 连接到服务器。 当我尝试向服务器进行身份验证时,我在客户端收到一条错误消息,显示从传输流中收到了意外的 EOF 或 0 字节。 在服务
我用 C# 制作了一个简单的 ftp 客户端,它可以满足我的需要(连接到 ftp,可以选择使用代理),但我也希望能够使用 AUTH SSL。 因此,我查看了 SslStream 而不是 Network
我有一个多线程异步System.Net.Socket,它监听一个端口。如果我收到来自 HTTP 的请求,我没有任何问题,但最近我不得不向我的应用程序添加 https 支持。 客户给了我一个.arm认证
我正在尝试编写一个客户端-服务器库,让我的 LAN 计算机相互通信。由于这在很大程度上是一种自学尝试,因此我试图通过 SSL 建立这些连接。我的计划是维护一台主机,并让每个客户端维护一个到主机的相互验
各位,我正在尝试写一些关于 SSL 的文章,这里是问题: 我在下面构建了一些东西: CA 证书(自制 CA) Server pfx, Server cert, Server key(由自制的CA签发到
我正在尝试对存在证书交换的客户端进行身份验证。我遇到的问题是 openssl 客户端拒绝连接尝试,因为身份验证握手中没有包含任何证书颁发机构。 这对于在 Windows Server 2008 R2
我正在尝试连接到 SSL TCP 服务器,它可以在我的浏览器上运行,并且我可以收到硬编码的响应。在应用程序上,当使用 ReadLine 函数时抛出异常。 Error: System.ArgumentE
伙计们,我正在使用 SslStream 作为服务器来测试我的应用程序,但我在从流中读取时遇到了问题。我正在使用以下代码: while (true) {
我正在设计一个基于客户端服务器架构的系统 - 基于 TCP。要求服务器和客户端之间的所有消息都应该加密。所以我正在考虑在 .NET Framework 中使用 SslStream 类。 从 SslSt
我有这个代码: string certificateFilePath = @"C:\Users\Administrator\Documents\Certificate.pfx"; string cer
我看了很长时间的 MSDN 信息文章,但我仍然无法理解。 基于不需要客户端身份验证的假设: 1.当我调用SslStream.AuthenticateAsServer(...)时,我是在服务器端还是在客
我对处理套接字时“最好”使用什么感到困惑。 Socket 对象提供 Send/Receive 方法(和异步等效方法),但也允许创建 NetworkStream。我对使用 Socket.Send 感到高
我正在开发 C# SSL 客户端。 这是单向证书验证。我的客户需要进行服务器证书验证。 我已将 RemoteCertificateValidationCallback 添加到 SslStream。 目
我目前正在尝试使用 TcpClient 连接到 FTP 服务器。 为了使用 FTPS 作为协议(protocol),我通过 NetworkStream 处理一个单独的方法,该方法使用该方法创建一个 S
当使用 SSLStream 向(已经过身份验证的)客户端发送“大”数据 block (1 兆)时,我看到的数据包碎片/分解是 远大于使用普通 NetworkStream 时。 在客户端使用异步读取(即
我是一名优秀的程序员,十分优秀!