简体   繁体   English

以ASP.net页面启动服务

[英]Start a Service thought ASP.net Page

I wrote a windows service, that works perfectly. 我写了一个Windows服务,效果很好。 It is call (on Command Prompt) via ChatServer.exe {argument} where the {argument} is a key work such as install , uninstall , start and stop . 它是通过ChatServer.exe {argument}调用的(在命令提示符下),其中{argument}是诸如installuninstallstartstop类的关键工作。

The program where this service is require administrative privileges (since it install/uninstall itself). 该服务所在的程序需要管理特权(因为它本身可以安装/卸载)。 So if i start cmd as administrator "D:\\folder\\chatserver.exe install" for example, it install the service as it should. 因此,例如,如果我以管理员"D:\\folder\\chatserver.exe install"身份启动cmd ,它将"D:\\folder\\chatserver.exe install"安装该服务。

Well, my problem is that on my ASP.net site i wrote a function (below) to start the process, but i get an exception "740" ("the software required privilege elevation") as if i mark the "AsAdmin" argument of my function to "true", i get that "UseShellExecute" can't be true as an exception. 好吧,我的问题是我在ASP.net站点上编写了一个函数(如下所示)来启动该过程,但出现异常"740" ("the software required privilege elevation") ,就像我标记了“ AsAdmin”自变量一样我的功能为“真”,我得到"UseShellExecute"不能为真是一个例外。

public static int RunProcess(string ApplicationPath, string Parameters = "", bool AsAdmin = false)
{
    try
    {
        global::System.Diagnostics.ProcessStartInfo startInfo = new global::System.Diagnostics.ProcessStartInfo();
        startInfo.UseShellExecute = AsAdmin;
        if (AsAdmin) { startInfo.Verb = "runas"; }
        startInfo.WorkingDirectory = global::System.IO.Path.GetDirectoryName(ApplicationPath);
        startInfo.FileName = ApplicationPath;
        if (!string.IsNullOrEmpty(Parameters)) { startInfo.Arguments = Parameters; }
        startInfo.ErrorDialog = false;
        global::System.Diagnostics.Process process = global::System.Diagnostics.Process.Start(startInfo);
        process.WaitForExit();
        return process.ExitCode;
    }
    catch (global::System.ComponentModel.Win32Exception ex) { return ex.NativeErrorCode; }
    catch { return -1; }
}

What do i do? 我该怎么办?

Have you tried ProcessStartInfo ? 您是否尝试过ProcessStartInfo It allows you to add specific credentials . 它允许您添加特定的credentials Check the example below: 检查以下示例:

ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.WorkingDirectory = @"C:\Windows\System32";
myProcess.UseShellExecute = false;
// elevate EDIT
myProcess.Verb = "runas";
Process.Start(myProcess);


private static SecureString MakeSecureString(string text)
{
     SecureString secure = new SecureString();
     foreach (char c in text)
     {
         secure.AppendChar(c);
     }
     return secure;
}

Launch a process under another user's credentials 在另一个用户的凭据下启动进程

You can't, and shouldn't want to do this. 您不能也不应该这样做。 You don't want to store administrative credentials anywhere near your web application, and you definitely don't want to run your web application under administrative privileges. 您不想将管理凭据存储在Web应用程序附近的任何位置,并且绝对不想在管理特权下运行 Web应用程序。

One solution is to actually have a "watchdog" Windows Service, running with the appropriate privileges to interact with the Service Control Manager (SCM), which accepts commands through for example WCF on localhost, and let your web application talk to that service which in turn starts or stops the the appropriate service. 一种解决方案是实际上拥有一个“看门狗” Windows服务,该服务以适当的特权运行以与服务控制管理器(SCM)进行交互,该服务控制器通过localhost上的WCF接受命令,并让您的Web应用程序与该服务进行通讯,打开或停止适当的服务。

That would look like this: 看起来像这样:

[Web Application] -- WCF --> [Watchdog Service] -- SCM --> [Chat Service]

So your Web Application sends through WCF a StartService("ChatService") command, and then the Watchdog Service starts the ChatService service. 因此,您的Web应用程序通过WCF发送StartService("ChatService")命令,然后看门狗服务启动ChatService服务。

Now only the Watchdog Service has to run under administrative privileges, and to secure the WCF communication to make sure only authenticated applications call it, that's discussed in other questions. 现在,只有Watchdog Service才能在管理特权下运行,并确保WCF通信的安全,以确保只有经过身份验证的应用程序才能调用它,这在其他问题中进行了讨论。

If instead you are trying to develop a self-installing web platform including websites and services, then consider using a proper installer instead of doing it all manually. 相反,如果您尝试开发包括网站和服务的自安装Web平台,请考虑使用适当的安装程序,而不是手动完成所有操作。

Further to Ted's answer, I would add this: 除了Ted的回答,我将添加以下内容:

Microsoft does not recommend calling an .exe from a Web application/site as w3wp.exe runs in a sandboxed environment for security reasons and hence any thread/task/process that it launches is not the same as it would be when you launch it yourself and hence may not work as expected. Microsoft不建议从Web应用程序/站点调用.exe,因为出于安全原因w3wp.exe在沙盒环境中运行,因此它启动的任何线程/任务/进程都与您自己启动它的方式不同。因此可能无法正常工作。

You may want to re-code the console applications as ASP.NET Web API, possibly hosted in IIS or in a Windows Service. 您可能希望将控制台应用程序重新编码为ASP.NET Web API,可能托管在IIS或Windows Service中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM