使用ServiceController和模拟启动远程Windows服务

Dav*_*ner 5 .net impersonation windows-services servicecontroller

我有一个.NET MVC3应用程序,该应用程序需要能够打开和关闭远程服务。为此,我通过WindowsIdentity.Impersonate()模拟了一个特定的用户帐户。要测试用户的权限,我可以以用户身份登录并sc.exe \\[server] start [service]在命令提示符下执行。我还知道模仿命令可以按预期工作,因为该应用程序以匿名方式运行,因此无法在.没有模仿的情况下控制本地计算机()上的服务,但可以通过模仿控制本地服务。但是,当我将它们放在一起并尝试启动远程服务而不是本地服务时,总是出现错误“无法[service]在计算机' [server]' 上打开服务”

有没有人遇到过类似的问题?我一直以为它是服务器配置而不是.NET问题,直到我意识到sc.exe可以正常工作。这是我正在使用的类的缩写版本:

public class Service
{
    public string Name;
    public bool Running;
    private ServiceController serviceController;

    public Service(string name, string host)
    {
        Name = name;

        serviceController = new ServiceController(Name, host);
        Running = serviceController.Status == ServiceControllerStatus.Running;
    }

    public bool StartService()
    {
        ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, serviceController.MachineName, Name);
        scp.Assert();

        serviceController.Start();
        serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 5));
        serviceController.Refresh();

        Running = serviceController.Status == ServiceControllerStatus.Running;

        return Running;
    }
}
Run Code Online (Sandbox Code Playgroud)

补充说明:如果我不是将服务器指向域上的另一台Windows 7 PC,而是将模拟凭据更改为该PC所有者的凭据,则实际上可以远程控制其服务,而不会出现问题。

根据请求,我在此处添加模拟代码。再长一点,所以请忍受:

public class Impersonate
{
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    WindowsImpersonationContext impersonationContext;

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    public bool impersonateValidUser(String userName, String domain, String password)
    {
        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return false;
    }

    public void undoImpersonation()
    {
        impersonationContext.Undo();
    }
}
Run Code Online (Sandbox Code Playgroud)

在尝试启动或停止服务之前,我调用此代码:

Service s = new Service(ServiceName, MachineName);

if (Impersonation.impersonateValidUser(Username, Domain, Password))
{
    if (s.Running)
        s.StopService();
    else
        s.StartService();

    Impersonation.undoImpersonation();
}
Run Code Online (Sandbox Code Playgroud)

可能值得注意的是,我可以列出服务并获得单个服务的状态(就像我在这里所做的一样)-只有当我启动或停止服务时,我才会遇到麻烦。

小智 3

也许我已经找到了答案:

在模拟方法LogonUserA中

使用LOGON32_LOGON_SERVICE (= 5) 而不是LOGON32_LOGON_INTERACTIVE

确保您模拟的用户与运行该服务的用户是同一用户。

就我而言,用户包含在本地策略 --> 将会话作为服务启动。我尚未对未包含在该本地策略中的用户进行测试。

希望能帮助到你!