如何从C#启动/停止Windows服务

问题描述 投票:-1回答:2

我在我的应用程序中有三个用户角色。

  1. 超级管理员
  2. 管理员
  3. 用户

我想仅为具有SuperAdmin用户角色的用户启动/停止服务。

应用程序的需求是从ASP.NET Web应用程序启动/停止服务(或运行您要启动/停止的任何其他服务)。

c# asp.net service windows-services
2个回答
3
投票

使用Service Controll Manager启动/停止服务,而不是SHUTDOWN T-SQL动词。 SCM了解服务依赖性,并将正确关闭SQL Agent等依赖服务。此外,只有SCM才能启动服务。 SCM的托管API是ServiceController类。这解决了启动/停止服务的问题。

现在,关于冒充。为什么要关心?确定用户是否是“superadmin”(无论这意味着)是否是您的应用程序特定逻辑。在ASP.Net逻辑中进行身份验证和授权,然后授予应用程序池主体启动和停止服务所需的权限。见Service Security and Access Rights。请注意本地身份验证用户如何具有连接到SCM和启动/停止服务所需的所有权限。

如果您确实选择模拟,那么它将落在模拟的上下文中以获得必要的权限。在Windows中没有“超级管理员”这样的概念,您将模拟Active Directory帐户,该帐户将具有或不具有连接到SCM和启动/停止服务的权限。


0
投票

我在上面的场景中做了什么,我必须使用c#从Asp.Net Web应用程序启动我的监控服务器服务。

添加System.ServiceProcess默认Start(); Stop(); Refresh();函数

Code Behind

using System.ServiceProcess;

private readonly ServiceController _monitoringServer = new ServiceController("MONITORING$SERVER");

// on login screen, I just saved the userRole of a Loggedin user in Session["userRole"]

// On pageload, refresh the sql service and gets its current status.
protected void Page_Load(object sender, EventArgs e)
{
    _monitoringServer.Refresh();
    GetServiceCurrentStatus();

    // if userRole of loggedIn user is SuperAdmin, then it will make the buttons visible to the user
    // else it will hide the buttons and show only status of SQL SERVER to admin user and standard/limited user
    if(Session["userRole"] == "SuperAdmin"){
        this.btnStartServer.Visible = true;
        this.btnStopServer.Visible = true;
    }
    else{
        this.btnStartServer.Visible = false;
        this.btnStopServer.Visible = false;
    }
    // You can further login to show hide Start Stop buttons
}

// Start Server Function
protected void btnStartServer_Click(object sender, EventArgs e)
{
    _monitoringServer.Refresh();

    if (_monitoringServer.Status == ServiceControllerStatus.Stopped)
    {
        _monitoringServer.Start();
        _monitoringServer.Refresh();
    }
    GetServiceCurrentStatus();
}

// Stop Server Function
protected void btnStopServer_Click(object sender, EventArgs e)
{
    _monitoringServer.Refresh();
    if (_monitoringServer.Status == ServiceControllerStatus.Running)
    {
        _monitoringServer.Stop();
        _monitoringServer.Refresh();
    }
    GetServiceCurrentStatus();
}

// Get Current Status of SQL Service Function
private void GetServiceCurrentStatus()
{
    try
    {
        _monitoringServer.Refresh();

        if (_monitoringServer.Status == ServiceControllerStatus.Running)
            this.txtServerStatus.Text = "Monitoring Server: Running";
        else if (_monitoringServer.Status == ServiceControllerStatus.Stopped)
            this.txtServerStatus.Text = "Monitoring Server: Stopped";
        else if (_monitoringServer.Status == ServiceControllerStatus.StartPending)
                this.txtServerStatus.Text = "Monitoring Server: Starting...";
        else if (_monitoringServer.Status == ServiceControllerStatus.StopPending)
            this.txtServerStatus.Text = "Monitoring Server: Stopping...";
        else if (_monitoringServer.Status == ServiceControllerStatus.Paused || _monitoringServer.Status == ServiceControllerStatus.PausePending)
            this.txtServerStatus.Text = "Monitoring Server: Pause";
        else
            this.txtServerStatus.Text = "Monitoring Server: Processing";
    }
    catch (Exception)
    {
        this.txtServerStatus.Text = "Monitoring Server: Not Installed";
    }
}

// Timer Function
protected void ServerTimer_OnTick(object sender, EventArgs e)
{
    _monitoringServer.Refresh();
}

Client Side

// On client side, I just used a timer and sets its interval to 2 seconds (2000ms)
// after every 2 seconds it executes the ServerTimer_OnTick function which refreshes the status
<asp:Timer ID="ServerTimer" runat="server" Interval="2000" OnTick="ServerTimer_OnTick"></asp:Timer>
© www.soinside.com 2019 - 2024. All rights reserved.