Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 远程服务管理_C#_Service_Impersonation - Fatal编程技术网

C# 远程服务管理

C# 远程服务管理,c#,service,impersonation,C#,Service,Impersonation,我正在尝试使用ServiceController在另一台计算机上管理服务 var sc = new ServiceController(serviceName, machine); Console.WriteLine(sc.Status); 由于我需要使用不同的凭据,我使用以下方式执行模拟: var tokenHandle = IntPtr.Zero; bool returnValue = LogonUser(userName,

我正在尝试使用ServiceController在另一台计算机上管理服务

        var sc = new ServiceController(serviceName, machine);
        Console.WriteLine(sc.Status);
由于我需要使用不同的凭据,我使用以下方式执行模拟:

        var tokenHandle = IntPtr.Zero;
        bool returnValue = LogonUser(userName, domainName, password,
                                     LOGON32_LOGON_NEW_CREDENTIALS,
                                     LOGON32_PROVIDER_DEFAULT,
                                     ref tokenHandle);            
        if (!returnValue)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        WindowsIdentity newId = new WindowsIdentity(tokenHandle);
        impersonatedUser = newId.Impersonate();
模仿似乎有效。但我一直收到一个残疾手术例外:

System.InvalidOperationException: Cannot open MyService service on computer 'TargetMachine'. 
---> System.ComponentModel.Win32Exception: Access is denied
我的工作站位于域上,而目标计算机参与工作组

你知道我在这里遗漏了什么吗

我的工作站位于域上,而目标计算机参与工作组


这是你的问题。您正在使用本地或域帐户进行模拟。它们不是目标计算机的一部分,因为它位于工作组中。

访问被拒绝。当我没有管理员权限的时候我就有了。。在管理员模式下运行VS。如果它能工作,那么你只需要给你的程序管理员权限。它非常适合本地服务。我还使用[PermissionSetAttribute(SecurityAction.Demand,Name=“FullTrust”)]模拟一个仅存在于工作组计算机上的用户。模拟成功了,我还收到了有关服务的一些详细信息(名称和描述),但由于“访问被拒绝”,其他一切都不可能。