• 网页调用服务程序


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

  • 相关阅读:
    golang中的值传递和引用传递
    链表

    hashtable(哈希表)
    TAO: Facebook’s Distributed Data Store for the Social Graph. 论文阅读笔记(上)
    Skip Lists: A Probabilistic Alternative to Balanced Trees 跳表论文阅读笔记
    【译】如何实现一个现代化电子商城搜索?(一)
    Elasticsearch搜索资料汇总
    Python 按比例获取样本数据或执行任务
    Python 按分类样本数占比生成并随机获取样本数据
  • 原文地址:https://www.cnblogs.com/stonespawn/p/1450285.html
Copyright © 2020-2023  润新知