yme*_*rej 5 c# impersonation windows-services process localsystem
我有以下简单的服务程序:
using System.Diagnostics;
using System.ServiceProcess;
namespace BasicService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
Verb = "runas",
UserName = "jdoe",
Password = "XXXXXXX".ConvertToSecureString(),
Domain = "abc.com",
UseShellExecute =false,
FileName = "notepad.exe"
};
Process.Start(processStartInfo);
}
protected override void OnStop()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用它作为我的服务安装程序:
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace BasicService
{
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private readonly ServiceProcessInstaller _process;
private readonly ServiceInstaller _service;
public ProjectInstaller()
{
_process = new ServiceProcessInstaller {Account = ServiceAccount.LocalSystem};
_service = new ServiceInstaller
{
ServiceName = "BasicService",
Description = "Just a testing service.",
StartType = ServiceStartMode.Automatic,
};
Installers.Add(_process);
Installers.Add(_service);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在没有动词,用户名,密码,域和useshellexecute指定的情况下运行此服务,一切都运行得很花哨.只要我如上所述指定这些值,我就会得到以下结果:
服务无法启动.System.ComponentModel.Win32Exception(0x80004005):在Basic.Service1的System.Diagnostics.Process.Start(ProcessStartInfo startInfo)上的System.Diagnostics.Process.Start()处的System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒绝访问.OnStart(String [] args)在C:\ BasicService\BasicService\Service1.cs:System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)中的第24行
有任何想法吗?
从 Windows Vista 开始,服务不能简单地显示 ui 或与用户交互。
http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
因此,要从您的服务运行 GUI 应用程序,您需要使用 CreateProcessAsUser,它在 .NET 中不能直接使用。所以你必须依赖 Pinvoke,有点类似于这里描述的
| 归档时间: |
|
| 查看次数: |
4596 次 |
| 最近记录: |