N长时间都没有写博客了,似乎将自己松懈了,还是工作忙了,还是其他繁琐之事?前几天做一个小的功能,就是在web页面调用系统服务,或者调用自己的服务程序。一些心得和大家分享一下,网上的相关知识点也比较少,MSDN上有很多,但是英文较差的我又点吃力。
原以为简单的调用便可,如下:
try {
ServiceController sc = new ServiceController("IIsWebVirtualDir");
if (sc.Status == ServiceControllerStatus.Running)
sc.Stop();
else
sc.Start();
}
但是在IIS下查看结果:
可是在VS中调试并没用这种异常,可是为什么是这样呢?我想应该是在IIS中查看,是因为IIS登录用户应该没有权限去启动或者运行服务程序,当VS调试时,此时的用户应该是Administrator用户,当然有足够的权限去开启服务或者停止服务。
在查看MSDN之后,得知在IIS中可以模拟管理员登录,便可掉用系统服务了。(在ASP.NET应用程序中使用身份模拟(Impersonation)) 当然介绍了很多方法:
1、ASP.NET中的身份模拟
2、模拟IIS认证帐号
3、在某个ASP.NET应用程序中模拟指定的用户帐号
4、在代码中模拟IIS认证帐号
5、在代码中模拟指定的用户帐号
这些资料地址:http://www.microsoft.com/China/Community/program/originalarticles/TechDoc/impersonation.mspx
客户端调用服务程序,必须是IIS用户登录,那此时的IIS用户的权限是不够的,必须在代码中模拟指定的用户帐号进行登录具体的程序如下://这些定义的常量是登录的模式,如没有桌面的,安全模式等等
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
public const int LOGON32_LOGON_SERVICE = 5;
public const int LOGON32_LOGON_BATCH = 4;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
private bool impersonateValidUser(String userName, String domain, String password)
{ WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (LogonUser(userName, domain, password, LOGON32_LOGON_BATCH,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
return true;
else
return false;
}
else
return false;
}
else
{
int flag= GetLastError();
return false;
}
}
[DllImport("kernel32.dll")]
public static extern Int32 GetLastError();
private void undoImpersonation()
{
impersonationContext.Undo();
}
protected void ButtonSure_Click(object sender, EventArgs e)
{
string username = this.txtUserName.Text;
string pwd = this.txtPassword.Text;
if (pwd == "")
{
this.Label1.Text = "此服务要求,计算机系统管理员不能使用空密码,需填写密码!";
}
else
{
if (impersonateValidUser(username, ".", pwd))
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\sbin");
try
{
ServiceController sc = new ServiceController("SstAppService");
if (sc.Status == ServiceControllerStatus.Running)
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Start();//此时可以调用服务程序
this.Panel1.Visible = false;
Response.Redirect("WebBSoftWare.aspx");
}
catch (Exception ex)
{
this.Label1.Text = ex.ToString();
}
// Insert your code that runs under the security context of a specific user here.
undoImpersonation();
}
else
{
this.Label1.Text = "管理员模拟登录失败,请确认系统管理员用户名和密码!";
}
}
}