如何配置防火墙以允许RPC

时间:2019-07-28 08:59:31

标签: c# iis rpc windows-firewall

我正在尝试使用IIS远程更改C#应用程序池标识(用户)并出现错误

  

System.Runtime.InteropServices.COMException(0x800706BA):RPC服务器不可用。

如果我允许来自远程计算机上所有服务的所有RPC动态端口(在49152到65535范围内)都来自防火墙,则我可以正确执行此操作。 我只想知道远程系统用来完成该过程的确切服务或进程名称,以便只允许该服务的端口使用。

public static bool ChangeAppPoolUser(string ip, string machineName, string username, string password, string applicationPoolName)
{          
    try
    {
        var metabasePath = "IIS://" + ip + "/W3SVC/AppPools";
        // Get list of appPools at specified metabasePath location 
        using (DirectoryEntry appPools = new DirectoryEntry(metabasePath, username, password))
        {
            if(appPools==null)
            {
                Helper.PrepareDebugLog("appPools is null");
            }
            Helper.PrepareDebugLog("metabasePath:" + metabasePath + " username:" + username + " password:" + password);

            // From the list of appPools, Search and get the appPool  
            using (DirectoryEntry AppPool = appPools.Children.Find(applicationPoolName, "IIsApplicationPool"))
            {
                Helper.PrepareDebugLog("in");

                if (AppPool != null)
                {                            
                    AppPool.InvokeSet("AppPoolIdentityType", new Object[] { 3 });

                    // Configure username for the AppPool with above specified username                     

                    AppPool.InvokeSet("WAMUserName", new Object[] { Environment.UserDomainName + "\\" + Environment.UserName });

                    // Configure password for the AppPool with above specified password                       
                    AppPool.InvokeSet("WAMUserPass", new Object[] { CommonProgramVariables.localPassword });

                    // Write above settings to IIS metabase 
                    AppPool.Invoke("SetInfo", null);

                    // Commit the above configuration changes that are written to metabase 
                    AppPool.CommitChanges();
                    return true;
                }
            }
        }
    }
    catch (Exception e)
    {
        Helper.PrepareLogWithTimstamp("EXCEPTION WHILE CHNAGE USER: Parameter USED machineName:" + machineName + " username:" + username + " password:" + password + " applicationPoolName:" + applicationPoolName + " LocalPassword:" + CommonProgramVariables.localPassword + " Local User:" + Environment.UserDomainName + "\\" + Environment.UserName);
        Helper.PrepareLog("EXCEPTION:", e);
    }
    return false;
}

预期:AppPool用户应更改为远程计算机AppPool。

实际结果:

  

System.Runtime.InteropServices.COMException(0x800706BA):RPC服务器不可用。

1 个答案:

答案 0 :(得分:1)

错误RPC服务器不可用。如果由于防火墙限制而在目标计算机上阻止了RPC / WMI连接,或者您输入的目标计算机的主机名/ IP地址不正确,则会发生(HRESULT异常:0x800706BA)。

要解决此错误,您可以按照以下步骤操作:

1)打开控制面板,单击安全,然后单击Windows防火墙。

2)单击“更改设置”,然后单击“例外”选项卡。

3)在“例外”窗口中,选中Windows Management Instrumentation(WMI)的复选框以启用通过防火墙的WMI通信。 enter image description here