网页调用服务程序

摘要:
几天前,一个小功能是调用系统服务或在网页上调用自己的服务程序。我认为应该在IIS中查看,因为IIS登录用户不应有启动或运行服务程序的权限。在VS调试时,此时的用户应该是管理员用户,当然有足够的权限启动或停止服务。检查MSDN后,您可以以IIS管理员身份登录,然后可以删除系统服务。
   
       N长时间都没有写博客了,似乎将自己松懈了,还是工作忙了,还是其他繁琐之事?前几天做一个小的功能,就是在web页面调用系统服务,或者调用自己的服务程序。一些心得和大家分享一下,网上的相关知识点也比较少,MSDN上有很多,但是英文较差的我又点吃力。
 
        场景1:我在客户端做了一个服务程序,当机器一启动,程序就开始运行,假定为:Server.exe

        场景2:客户端人员需要通过web页面能够控制服务程序Server.exe的启动、运行和停止。

原以为简单的调用便可,如下:
try {
 ServiceController sc = new ServiceController("IIsWebVirtualDir");
      if (sc.Status == ServiceControllerStatus.Running)
          sc.Stop();
      else
          sc.Start();
}
但是在IIS下查看结果:网页调用服务程序第1张

 

可是在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 = "管理员模拟登录失败,请确认系统管理员用户名和密码!";
    }
  }
}

免责声明:文章转载自《网页调用服务程序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HTML-tableSQL Serve允许远程连接的解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【转帖】C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用

1 DLLImport的使用 using System; using System.Runtime.InteropServices; //命名空间 class Example { //用DllImport 导入Win32的MessageBox函数 [DllImport("user32.dll", CharSet = CharSet.Unicode)] p...

IIS7.5 伪静态 脚本映射 配置方法

首先,是IIS7.0的配置,由于Windows Server 2008操作系统默认的IIS版本为7.0,我们知道,IIS7.0与IIS6.0 核心注意的地方:先要将应用池设置为集成模式,修改OK后,再改成经典模式。否则,什么托管程序出不来。剩下的按照这个模式操作就可以了 的操作界面差别较大,如图所示:1. IIS7.0操作界面: 2 新建网站同时新建应用程...

win7安装iis错误解决方法汇总

错误IIS 7 运行出现 500.19 Error 的解决方法 解决方法 HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 overrideMode="...

C#直接打印word文档

    using using Microsoft.Office.Interop.Word;    (通过添加引用-com组件,找office的word组件  /// <summary>        /// 打印word        /// </summary>        /// <param name="filepa...

.NET应用程序7种最常见的性能问题及其解决方案

译者注:这篇文章依然是介绍.NET Framework框架下的性能问题排查,可能并不直接适用于.NET Core,但有时也能提供一些参考。   .NET应用程序7种最常见的性能问题及其解决方案 原文地址:https://www.eginnovations.com/blog/top-7-net-application-performance-problems...

IIS自动安装程序(免费)

本文转载:http://www.cnblogs.com/cunet/archive/2008/10/31/1323896.html IIS的安装对于非专业人士来说是件难以完成的任务,通常不知道如何着手,且安装过程繁琐,易出错,还需要操作系统安装光盘。为了尽可能简化IIS的安装,蓝点软件于2008年10月研究制作了这个全系列IIS自动安装程序。下载解压后直接...