远程机器上的C#服务状态
我是一个专业的程序员,因此,我不知道我在做什么:) 严肃地说;不,我绝对不是专家。不过我确实有个问题,不知道如何解决。好的是,我(想我)知道问题出在哪里,我希望这里的人能帮上忙 这是问题的概要。我正在用C#创建一个表单,它将为我完成一些服务器和数据库管理任务。我有一个按钮,当点击时,应该返回“y”服务器上“x”服务的服务状态。状态在屏幕上打印到文本框中 这是我的密码:远程机器上的C#服务状态,c#,servicecontroller,C#,Servicecontroller,我是一个专业的程序员,因此,我不知道我在做什么:) 严肃地说;不,我绝对不是专家。不过我确实有个问题,不知道如何解决。好的是,我(想我)知道问题出在哪里,我希望这里的人能帮上忙 这是问题的概要。我正在用C#创建一个表单,它将为我完成一些服务器和数据库管理任务。我有一个按钮,当点击时,应该返回“y”服务器上“x”服务的服务状态。状态在屏幕上打印到文本框中 这是我的密码: private void button2_Click(object sender, EventArgs e)
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());
}
}
private void按钮2\u单击(对象发送者,事件参数e)
{
string fs=“服务X状态=”;
字符串mr=“服务A状态=”;
字符串qp=“服务B状态=”;
string sp=“后台处理程序服务状态=”;
ServiceController fssc=新的ServiceController(“xService”、“yServer”);
ServiceController mrsc=新的ServiceController(“aService”、“yServer”);
ServiceController qpsc=新的ServiceController(“bService”、“yServer”);
ServiceController spsc=新的ServiceController(“假脱机程序”、“yServer”);
尝试
{
AppendText(sp+spsc.Status.ToString());
AppendText(Environment.NewLine);
AppendText(fs+fssc.Status.ToString());
AppendText(Environment.NewLine);
AppendText(mr+mrsc.Status.ToString());
AppendText(Environment.NewLine);
AppendText(qp+qpsc.Status.ToString());
}
捕获(异常垃圾)
{
字符串msg=“”;
int i;
对于(i=0;i
我遇到异常,基本上是说:无法在“服务器”上打开“服务”。由于这是一个远程服务器,我假设这是一个凭据/安全问题。但是,我对后台处理程序服务没有任何问题
我的问题是…我如何将用户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();
好的,我已经找到了如何做到这一点,但我不太明白如何编码。根据我所读到的,我需要将我想在上面做的事情封装在一些模拟用户ID的代码中,然后在它获得请求的信息后,撤销模拟。我理解,硬编码用户信息不是最安全的方式,但足以满足其用途。我将继续研究如何在C#中模拟。这是否只适用于所有内容都连接到域控制器的环境?只有在域控制器之间建立信任时,这才适用于跨域。