【问题标题】:C# Service Status On Remote Machine远程机器上的 C# 服务状态
【发布时间】:2011-07-28 21:41:58
【问题描述】:

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

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

这是问题的概要。我正在用 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 和密码传递给该服务器,以便它进行身份验证或运行,以便我可以检查这些服务的状态,这就是问题所在。如果有人不认为这是问题所在,请告诉我我哪里出错了:)

【问题讨论】:

  • 好的,我已经找到了如何做到这一点,但我不太明白如何编码。根据我的阅读,我需要将我想要在上面做的事情包装在一些模拟用户 ID 的代码中,然后在它获取我请求的信息之后,撤消模拟。我知道对用户信息进行硬编码并不是最安全的方法,但足以满足其用途。我会继续研究如何在 C# 中进行模拟。

标签: c# servicecontroller


【解决方案1】:

终于明白了……

新建了一个类,如下图:

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

【讨论】:

  • 这只适用于所有东西都连接到域控制器的环境中吗?
  • 只有在它们之间建立了信任时,这才会跨域工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多