I have this code, which establishes a SSL encrypted Stream to a server over TCP:
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);
}
Which works perfectly fine when running as a regular Console app as Administrator
user.
However the same code keeps looping in the while loop
meant for reading the first 4 bytes of the stream when the app is registered and ran as a Windows Service (in Session 0), as Local System
user.
While debugging, as the Console app, the while loop
receives all 4 bytes on the first loop and exits the loop right after the first turn, however when ran as a Windows Service, stream doesn't receive any bytes (read
is always 0) and just keeps looping forever.
Code is running on a Windows Server 2012 R2 machine with latest updates installed, on .Net Framework v4.6.2.
Any hint is highly appreciated.