Search code examples
.netwindowstls1.2windows-server-2022

.Net console app works as user, not while using any other type of startup


I've been breaking my head over this for a day of two now.

I have a relatively simple (yet old, but updated to .net 4.8) .net Console app which connects to Broadsoft with simple http requests. When I start it up manually, it get's the proper handshake with the server on the other side using TLS 1.2.

However, when started through Powershell, Python or Batch, it will result in "A fatal error occurred while creating a TLS client credential. The internal error state is 10013"

This means the TLS handshake isn't correct. Yeah... Ok...

But why?

I've tried everything, from setting PS sessions with the TLS12 flag (only applicable on the PS session itself, not the .exe started by), to elevated rights or starting as specific user with interact rights to desktop.

Setup; server 2022 standard, TLS 1.0 & 1.1 disabled through policy (safest), .Net console application updated to 4.8 (including dependencies), working while operating manually, but not through automation.

Any advice welcome, thanks again.

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

Elevated rights (run as Administrator or myself)

Python script

Batch script

Update; py script

import subprocess
import time

# Path to the console program executable
console_program_path = "S:\\Broadsoft\\Broadsoft_CC\\Broadsoft_CC.exe"

# Open the prompts text file
with open("S:\\Broadsoft\\Parameters\\Parameters_CC.txt", "r", encoding="utf-16") as prompts_file:
    prompts = prompts_file.readlines()

# Run the console program using subprocess
with subprocess.Popen(console_program_path, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) as process:
    for index, prompt in enumerate(prompts):
        process.stdin.write(prompt)
        process.stdin.flush()

        # Wait for a limited time to read output from the subprocess
        timeout_seconds = 2  # Adjust this timeout as needed
        start_time = time.time()
        while time.time() - start_time < timeout_seconds:
            # Non-blocking read from stdout
            output = process.stdout.readline().strip()
            if output:
                print(output)
                break  # Exit loop if output is read
            time.sleep(0.1)  # Wait a short interval before trying again

        # Add an "Enter" keypress after the last line
        if index == len(prompts) - 1:
            process.stdin.write("Start()\n")
            process.stdin.flush()

# Close the process and streams
process.stdin.close()
process.wait()    

Solution

  • Solved; what happened was that VS2022 did not update the app.config on the console app. I have no idea why it made such a difference for the service account or my own in this behaviour. IMO it should block the console app I started as well as the automated one.

    So, If you encounter this, always check your app.config to at least support .net 4.5 (and make sure it does)

    <startup><supportedRuntime version="v4.8" sku=".NETFramework,Version=v4.8" /></startup></configuration>