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

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
Cloudbric
Cloudbric
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
S
Schneier on Security
L
LangChain Blog
Jina AI
Jina AI
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
T
Tenable Blog
B
Blog RSS Feed
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
W
WeLiveSecurity
I
InfoQ
The Hacker News
The Hacker News
雷峰网
雷峰网
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
P
Privacy International News Feed
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
V
V2EX
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Cybersecurity and Infrastructure Security Agency CISA
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog

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