C#强制开启Windows系统远程桌面服务,取消仅允许运行使用网络级别身份验证(NLA)的远程桌面的计算机连接限制
|
admin
2025年3月7日 12:14
本文热度 912
|
以下是实现强制开启Windows 远程桌面服务并禁用网络级别身份验证(NLA)的C#代码示例:
using System;
using Microsoft.Win32;
using System.ServiceProcess;
using System.Diagnostics;
using System.Security.Principal;
class EnableRemoteDesktop
{
static void Main(string[] args)
{
if (!IsAdministrator())
{
Console.WriteLine("请以管理员权限运行此程序。");
return;
}
try
{
ConfigureTermService();
EnableRdpThroughRegistry();
DisableNlaThroughRegistry();
RestartTermService();
Console.WriteLine("远程桌面已成功启用,NLA限制已取消!");
}
catch (Exception ex)
{
Console.WriteLine($"操作失败: {ex.Message}");
}
}
static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
static void ConfigureTermService()
{
try
{
ProcessStartInfo scConfig = new ProcessStartInfo
{
FileName = "sc",
Arguments = "config TermService start= auto",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(scConfig)?.WaitForExit();
using (ServiceController service = new ServiceController("TermService"))
{
if (service.Status != ServiceControllerStatus.Running)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
}
}
}
catch (Exception ex)
{
throw new Exception($"配置远程桌面服务失败: {ex.Message}");
}
}
static void EnableRdpThroughRegistry()
{
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Control\Terminal Server", true))
{
key?.SetValue("fDenyTSConnections", 0, RegistryValueKind.DWord);
}
}
catch (Exception ex)
{
throw new Exception($"修改注册表启用RDP失败: {ex.Message}");
}
}
static void DisableNlaThroughRegistry()
{
try
{
using (RegistryKey rdpKey = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", true))
{
rdpKey?.SetValue("UserAuthentication", 0, RegistryValueKind.DWord);
}
}
catch (Exception ex)
{
throw new Exception($"禁用NLA失败: {ex.Message}");
}
}
static void RestartTermService()
{
try
{
using (ServiceController service = new ServiceController("TermService"))
{
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
}
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
}
}
catch (Exception ex)
{
throw new Exception($"重启服务失败: {ex.Message}");
}
}
}
使用说明:
以管理员身份运行:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
关键操作说明:
附加建议(可选):
static void AddFirewallRule()
{
try
{
ProcessStartInfo firewall = new ProcessStartInfo
{
FileName = "netsh",
Arguments = "advfirewall firewall add rule name=\"Remote Desktop\" dir=in protocol=TCP localport=3389 action=allow",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(firewall)?.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine($"防火墙规则添加失败: {ex.Message}");
}
}
注意事项:
相关文档:
C#强制关闭Windows Server系统服务器远程桌面服务方法[
3]
http://29680.oa22.cn
该文章在 2025/3/7 16:56:06 编辑过