惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
T
The Blog of Author Tim Ferriss
月光博客
月光博客
Recent Announcements
Recent Announcements
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
The Register - Security
The Register - Security
博客园 - 叶小钗
博客园 - Franky
The Cloudflare Blog
雷峰网
雷峰网
罗磊的独立博客
M
MIT News - Artificial intelligence
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
爱范儿
爱范儿
博客园 - 司徒正美
Recorded Future
Recorded Future
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
F
Full Disclosure
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog
大猫的无限游戏
大猫的无限游戏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
WordPress大学
WordPress大学
小众软件
小众软件
K
Kaspersky official blog
Attack and Defense Labs
Attack and Defense Labs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Forbes - Security
Forbes - Security
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
B
Blog RSS Feed
S
Security @ Cisco Blogs
美团技术团队
量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - stonespawn

Vue 组件库2 Vue 的组件库 算法小题目 VS2019 自动代码补全功能 GIT 删除操作 vue-router 注意事项 Vue中axios访问 后端跨域问题 Vue2.0 搭配 axios Vue 脚手架搭建 grunt使用入门 Git相关操作及记录 Ajax在调用含有SoapHeader的webservice方法 关于JS 树形结构 导出EXCEL【Web方式HTML通过拼接html中table】 链接点击跳动问题 WatiN——Web自动化测试(三)【弹出窗口处理】 WatiN——Web自动化测试(二) WatiN——Web自动化测试(一) 小问题 小技巧 :创建虚拟目录并将IIS里面.net配置版本设为2.0 - stonespawn
网页调用服务程序 - stonespawn - 博客园
stonespawn · 2009-05-05 · via 博客园 - stonespawn
   

       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下查看结果:

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