远程计算机上的 C# 服务状态

标签 c# servicecontroller

我是一名专业程序员,因此,我不知道我在做什么:)

严肃地说;不,我无论如何都不是专家。我确实有问题,但不知道如何解决。好消息是,我(认为我)知道问题出在哪里,我希望这里有人能提供帮助。

这是问题的概要。我正在用 C# 创建一个表单,它将为我执行一些服务器和数据库管理任务。我有一个按钮,单击该按钮应该返回“y”服务器上“x”服务的服务状态。状态打印在屏幕上的文本框中。

这是我的代码:

     private void button2_Click(object sender, EventArgs e)
    {
        string fs = "Service X Status = ";
        string mr = "Service A Status = ";
        string qp = "Service B Status = ";
        string sp = "Spooler Service Status = ";
        ServiceController fssc = new ServiceController("xService", "yServer");
        ServiceController mrsc = new ServiceController("aService", "yServer");
        ServiceController qpsc = new ServiceController("bService", "yServer");
        ServiceController spsc = new ServiceController("Spooler", "yServer");

        try
        {
            txtGtwySts.AppendText(sp + spsc.Status.ToString());
            txtGtwySts.AppendText(Environment.NewLine);
            txtGtwySts.AppendText(fs + fssc.Status.ToString());
            txtGtwySts.AppendText(Environment.NewLine);
            txtGtwySts.AppendText(mr + mrsc.Status.ToString());
            txtGtwySts.AppendText(Environment.NewLine);
            txtGtwySts.AppendText(qp + qpsc.Status.ToString());
        }
        catch (Exception crap)
        {
            string msg = "";
            int i;
            for (i = 0; i < crap.Message.Count(); i++)
            {
                msg += "Error # " + i + " Message: " + crap.Message + "\n";

            }
            MessageBox.Show(msg);
            MessageBox.Show(i.ToString());
        }
    }

我遇到异常,基本上是说:无法在“服务器”上打开“服务”。由于这是一个远程服务器,我假设这是一个凭据/安全问题。但是,我对 Spooler 服务没有任何问题。

我的问题是...我如何将用户 ID 和密码传递给此服务器,以便它进行身份验证或运行,以便我可以检查这些服务的状态,这就是问题所在。如果有人不认为这是问题所在,那么请告诉我哪里出了问题:)

最佳答案

终于明白了...

新建一个类,如下图:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Security.Permissions;

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);
        }            
    }        
}    

我发布的原始代码被包裹在:

ImpersonateUser iu = new ImpersonateUser();
iu.Impersonate("[domain]","[username]","[password]");
// code you want to execute as impersonated user.....
iu.Undo();

关于远程计算机上的 C# 服务状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6866104/

相关文章:

c# - 将像素数据转换为 Bitmap int array- WP8 - C#

c# - 为什么 XmlSerializer 不支持字典?

c# - .NET ServiceController.WaitForStatus 忽略超时

.net - 异步ServiceController.WaitForStatus如何做?

c# - 如何使用 ServiceController 远程控制 Windows 服务?

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

c# - LINQ 到 XML : Collapse mutliple levels to single list

c# - 检查 PropertyDescriptor 是否有属性

c# - Crystal Report 在加载时出现错误

.net - 获取 Windows 服务启动类型?