c# - 如何远程访问工作组计算机中远程主机的 Windows 服务?

标签 c# service wmi servicecontroller workgroup

我正在编写一个用于操作 Windows 服务的应用程序。我的计算机在工作组中。但我无法访问该机器。我尝试过使用 LogonUser() 进行模拟。但不工作。我可以使用远程桌面连接进行操作,但无法以编程方式访问。

    ImpersonateUser ImpersonatedUser = new ImpersonateUser();
    ImpersonatedUser.Impersonate(Domain, Username, password);
    ServiceController sc = new ServiceController("servicename", host_name);
    Console.WriteLine("Success."); 


  //impersonation.
public class ImpersonateUser
{
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(
    String lpszUsername,
    String lpszDomain,
    String lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public extern static bool CloseHandle(IntPtr handle);
    private static IntPtr tokenHandle = new IntPtr(0);
    private static WindowsImpersonationContext impersonatedUser;
    // If you incorporate this code into a DLL, be sure to demand that it
    // runs with FullTrust.
    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    public void Impersonate(string domainName, string userName, string password)
    {
        //try
        {
            // Use the unmanaged LogonUser function to get the user token for
            // the specified user, domain, and password.
            const int LOGON32_PROVIDER_DEFAULT = 0;
            // Passing this parameter causes LogonUser to create a primary token.
            const int LOGON32_LOGON_INTERACTIVE = 2;
            tokenHandle = IntPtr.Zero;
            // ---- Step - 1
            // Call LogonUser to obtain a handle to an access token.
            bool returnValue = LogonUser(
            userName,
            domainName,
            password,
            LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT,
            ref tokenHandle); // tokenHandle - new security token
            if (false == returnValue)
            {
                int ret = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(ret);
            }
            // ---- Step - 2
            WindowsIdentity newId = new WindowsIdentity(tokenHandle);
            // ---- Step - 3
            {
                impersonatedUser = newId.Impersonate();
            }
        }
    }
    // Stops impersonation
    public void Undo()
    {
        impersonatedUser.Undo();
        // Free the tokens.
        if (tokenHandle != IntPtr.Zero)
        {
            CloseHandle(tokenHandle);
        }
    }
} 

最佳答案

我发现我们无法模拟工作组计算机上的用户。尽管我们模拟,但工作组计算机会强制用户成为访客。因此,如果我们想以用户身份登录,我们需要更改注册表值forceguest=0

关于c# - 如何远程访问工作组计算机中远程主机的 Windows 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22555921/

相关文章:

安卓服务 - 错误 : service not registered

android - 7.0及以上版本应用被杀后重启服务

C++ 生成器 WMI : Invalid Query

java - 在java中使用SNMP和/或wmi/wmic进行进程到端口的映射

vb.net - 使用 ManagementObjectSearcher 时检测到 DisconnectedContext

c# - 我可以获得 C# Visual Studio 程序来控制树莓派吗?

c# - "using static"指令与Watch窗口不配合

c# - ThreadPool 在短短几秒钟内使用了过多的内存

c# - ASP.NET SqlDataSource 筛选器行为

android - 从电池性能影响的角度来看,SQLite 数据库与基于文件的数据存储