净SslStream工作正常桌面而不是在会话0(Windows服务会话)
问题描述:
我有这样的代码,通过TCP建立SSL加密流给服务器:净SslStream工作正常桌面而不是在会话0(Windows服务会话)
var client = new TcpClient(host, port);
var stream = new SslStream(client.GetStream(), false, ValidateServerCertificate);
var clientCertificates = new X509CertificateCollection {clientCertificate};
stream.AuthenticateAsClient(host, clientCertificates, sslProtocols, false);
var isAuthenticated = stream.IsAuthenticated; //This is true in both Console and Windows Service
var lenghtBytes = new byte[4];
int read = 0;
while (read < 4)
{
read = read + stream.Read(lenghtBytes, read, 4 - read);
}
这工作完全正常运行时,作为Administrator
用户的常规控制台应用程序。
但是,相同的代码在while loop
中保持循环,这意味着当应用程序注册并作为Windows服务(在会话0中)运行时读取流的前4个字节,如Local System
用户。
调试时,作为控制台应用程序,while loop
接收第一个循环中的所有4个字节并在第一个循环之后立即退出循环,但是当作为Windows服务运行时,流不会接收任何字节(read
总是0),并且永远持续循环。
代码在安装了最新更新的Windows Server 2012 R2计算机上运行.Net Framework v4.6.2。
任何提示高度赞赏。
答
当您作为本地系统运行时,根据根证书的可用性,代理设置等,ssl握手可能会运行错误,因为这些都是windows用户特定的。如果不调试问题,将很难理解问题的位置问题。
您可以使用sys内部工具PsExec在本地系统下运行任何进程。你必须使用的命令是psexec -s <programpath>
C:\windows\system32>psexec -s cmd.exe
PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\windows\system32>whoami
nt authority\system
一旦u必须运行在本地系统的工具,你可以对它进行调试,找出哪些是happening.If没有帮助,你也可以拍摄system.net this或this
希望它有帮助!
如果我理解正确,handshake和tcp通信工作正常,因为'stream.AuthenticateAsClient()'后有'stream.IsAuthenticated == true'。此外,我已经通过附加到服务并停止在断点上来作为服务运行时调试代码。在我的调试场景中'psexec'做了什么特别的事吗? –
不,它运行undr本地系统帐户。因此,您可以重现问题和debug.Are您能够在本地系统下运行时重现该问题? System.net跟踪将为您提供套接字上的原始数据详细信息。否则,您可能需要捕获[wireshark](https://www.howtogeek.com/104278/how-to-use-wireshark-to-capture- filter-and-inspect-packets /)trace –
我会给Wireshark一个尝试并将比较数据。 :) 感谢您指出了这一点。 –