首页 > 解决方案 > C# 如何从 Azure 应用服务 RDP 连接到本地 Windows 服务器?

问题描述

我有一个托管在 azure app 服务中的 Asp.net 核心 web api。我的 web api 应该连接到一个或多个 On Premise Windows 服务器以创建 DNS 记录。Windows 服务器正在运行 DNS 服务。我正在使用 System.Management 连接到本地 Windows 服务器。

但是,我没有成功从 azure app 服务连接到远程服务器。我确信我缺少某种配置,但我不知道..

这是我的代码:

using System.Management;

public static ManagementScope ConnectDNSServer(string dnsServerName, PCSCredential useMyInfo)
{
    ConnectionOptions options = new ConnectionOptions
    {
        Username = useMyInfo.WindowsDomain + "\\" + useMyInfo.UserName,
        Password = useMyInfo.PassString
    };

    try
    {
        ManagementScope scope = new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS", options);
        scope.Connect();
        return scope;
    }
    catch
    {
        //ManagementScope scopeEx = new ManagementScope();                
    }
    return null;
}

我收到此错误:

System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

标签: azurewmiazure-web-app-serviceremote-desktop

解决方案


在 Azure 应用服务上 - 无论地址如何,应用程序都无法使用端口 445、137、138 和 139 连接到任何地方。换句话说,即使连接到非私有 IP 地址或虚拟网络地址,也无法连接到端口不允许使用 445、137、138 和 139。

附加信息,标准/本机 Azure Web 应用在称为沙盒的安全环境中运行。每个应用程序都在自己的沙箱内运行,将其执行与同一台机器上的其他实例隔离开来,并提供额外的安全性和隐私性,否则这些应用程序将无法使用。所以,这似乎是由于沙盒。

在这种环境下,通过 Internet 访问应用程序的唯一方法是通过已经暴露的 HTTP (80) 和 HTTPS (443) TCP 端口;应用程序可能不会在其他端口上侦听来自 Internet 的数据包。与本地地址(例如 localhost、127.0.0.1)和机器自身 IP 的连接尝试将失败,除非同一沙箱中的另一个进程在目标端口上创建了侦听套接字: https ://github.com/projectkudu/kudu /wiki/Azure-Web-App-sandbox#network-endpoint-listening


推荐阅读