无法访问远程Windows服务

时间:2013-09-19 10:51:04

标签: c# asp.net windows-services

我有一个网站,用户可以在其中模拟/验证自己到远程服务器,并控制特定的Windows服务。如果我正在运行项目本地正常,但当我项目发布到 Web服务器 IIS服务器正在运行,它提供了一个例外:

无法在计算机上打开服务控制管理器(计算机名称)。此操作可能需要其他权限。

我的工作:

  1. 冒充(在当地运作良好)的课程:

    public class ImpersonationUtil
    {
        public static bool Impersonate()
        {
            string logon = "ADMIN";
            string password = "PASS";
            string domain = "THEDOMAIN";
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;
            WindowsImpersonationContext impersonationContext = null;
    
            if (LogonUser(logon, domain, password, 2, 0, ref token) != 0)
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    impersonationContext = new WindowsIdentity(tokenDuplicate).Impersonate();
    
            return (impersonationContext != null);
        }
    
        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    
        [DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        public extern static int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
    }
    
  2. 与服务连接的代码:

        ServiceController controller = new ServiceController("serviceName", "machineAddress");
        var status = controller.Status;
    
  3. Web.config stuff(要读取并接受Web服务器上的模拟):

    <system.webServer>     <validation validateIntegratedModeConfiguration="false"/> </system.webServer>

  4. 任何帮助?

1 个答案:

答案 0 :(得分:0)

更改

if (LogonUser(logon, domain, password, 2, 0, ref token) != 0)

if (LogonUser(logon, domain, password, 9, 0, ref token) != 0)

未经测试,但我认为它现在应该连接。