让程序自动以管理员身份运行

               

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Runtime.InteropServices;namespace Utility{    /// <summary>    /// 使用此类来模拟某个系统用户(系统帐号、AD等)    /// 主要用在需要特别权限的地方,因为IIS的系统帐号权限通常比较低,需要更高级权限时使用此类来替换用户,执行完毕后再换回原来的帐号    /// </summary>    public class Impersonal    {        [DllImport("advapi32.dll", SetLastError = true)]        public extern static bool LogonUser(String lpszUsername, String lpszDomain,            String lpszPassword, int dwLogonType,            int dwLogonProvider, ref IntPtr phToken);        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]        public extern static bool CloseHandle(IntPtr handle);        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,            int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);        const int LOGON32_PROVIDER_DEFAULT = 0;        const int LOGON32_LOGON_INTERACTIVE = 2;        const int SecurityImpersonation = 2;        private IntPtr tokenHandle;        private IntPtr dupeTokenHandle;        private System.Security.Principal.WindowsImpersonationContext impersonatedUser;        private string UserName;        private string PWD;        public Impersonal(string username, string password)        {            tokenHandle = new IntPtr(0);            dupeTokenHandle = new IntPtr(0);            UserName = username;            PWD = password;        }                /// <summary>        /// 开始模拟        /// </summary>        public void StartImpersonate()        {            string domainName = string.Empty;            string userName = string.Empty;                                    if (!System.Text.RegularExpressions.Regex.IsMatch(UserName, @"^/w+[//]?/w+$"))            {                throw new ApplicationException("非法的用户名");            }            string[] tmp = UserName.Split(new char[] { '//' });            if (tmp.Length > 1)            {                domainName = tmp[0];                userName = tmp[1];            }            else            {                userName = tmp[0];            }            tokenHandle = IntPtr.Zero;            dupeTokenHandle = IntPtr.Zero;            bool returnValue = LogonUser(userName, domainName, PWD,                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,                ref tokenHandle);            if (!returnValue)            {                throw new ApplicationException("取Handle出错了!");            }            //Console.WriteLine("当前用户是: "            //    + WindowsIdentity.GetCurrent().Name);            bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);            if (!retVal)            {                CloseHandle(tokenHandle);                throw new ApplicationException("复制Handle出错了!");            }            System.Security.Principal.WindowsIdentity newId = new System.Security.Principal.WindowsIdentity(dupeTokenHandle);            impersonatedUser = newId.Impersonate();        }        /// <summary>        /// 取消模拟        /// </summary>        public void StopImpersonate()        {            if (impersonatedUser != null)                impersonatedUser.Undo();            if (tokenHandle != IntPtr.Zero)                CloseHandle(tokenHandle);            if (dupeTokenHandle != IntPtr.Zero)                CloseHandle(dupeTokenHandle);        }    }}

前提你要有系统管理员的密码,如果客户端加入了域,就用域的管理员帐号登录。。

使用方法
Impersonal impl=new Impersonal(系统管理员帐号,密码);//例如..Impersonal("Administrator","12345")或者Impersonal("域名/Administrator","12345")
impl.StartImpersonate();
运行你的代码
impl.StopImpersonate(); 

 

我给你的类就是实现你想要的功能。用它来模拟管理员的身份,然后执行你想要的操作。

首先,你需要明白一点,你想要的“自动更改为以管理员身份运行”要有一个前提条件,就是你必须拥有管理员帐号的密码,在本机就是“Administrator”,在AD中就是 “域/Administrator”

你或者事先已经知道客户电脑的密码,或者弹出一个输入框让用户输入密码。然后:

Impersonal impl=new Impersonal(“Administrator”,用户输入的密码);
impl.StartImpersonate();
执行自动升级
impl.StopImpersonate();
 
比较简单的方式:
创建软件的快捷方式.
右击快捷方式并选择“属性”。
点击“Advanced”按钮,并勾选“Run as administrator”。
点“OK”保存更改。
然后:启动快捷方式就可。
System.Diagnostics.Process.Start(@"C:/Users/Jason/Desktop/xxx.lnk"); 

 

 

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值