.Net 控制台应用程序以用户身份运行,而不是在使用任何其他类型的启动时运行

问题描述 投票:0回答:1

我已经为此绞尽脑汁两天了。

我有一个相对简单(虽然很旧,但已更新到 .net 4.8)的 .net 控制台应用程序,它通过简单的 http 请求连接到 Broadsoft。当我手动启动它时,它会使用 TLS 1.2 与另一端的服务器进行正确的握手。

但是,当通过 Powershell、Python 或 Batch 启动时,会导致“创建 TLS 客户端凭证时发生致命错误。内部错误状态为 10013”

这意味着 TLS 握手不正确。是啊...好吧...

但是为什么呢?

我已经尝试了一切,从使用 TLS12 标志设置 PS 会话(仅适用于 PS 会话本身,不适用于启动的 .exe),到提升权限或以具有桌面交互权限的特定用户身份启动。

设置;服务器 2022 标准,通过策略禁用 TLS 1.0 和 1.1(最安全),.Net 控制台应用程序更新到 4.8(包括依赖项),在手动操作时工作,但不通过自动化。

欢迎任何建议,再次感谢。

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

提升权限(以管理员或我自己身份运行)

Python 脚本

批处理脚本

更新; py脚本

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()    
.net windows tls1.2 windows-server-2022
1个回答
0
投票

解决了;发生的情况是 VS2022 没有更新控制台应用程序上的 app.config。我不知道为什么它对服务帐户或我自己的行为产生如此大的影响。 IMO 它应该阻止我启动的控制台应用程序以及自动化的控制台应用程序。

所以,如果您遇到这种情况,请务必检查您的 app.config 以至少支持 .net 4.5(并确保它支持)

<startup><supportedRuntime version="v4.8" sku=".NETFramework,Version=v4.8" /></startup></configuration>
© www.soinside.com 2019 - 2024. All rights reserved.