让程序自动以管理员身份运行(用到了DuplicateToken,模拟管理员的身份,不可思议)

 

[c-sharp] view plain copy
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Runtime.InteropServices;  
  11.   
  12. namespace Utility  
  13. {  
  14.     /// <summary>  
  15.     /// 使用此类来模拟某个系统用户(系统帐号、AD等)  
  16.     /// 主要用在需要特别权限的地方,因为IIS的系统帐号权限通常比较低,需要更高级权限时使用此类来替换用户,执行完毕后再换回原来的帐号  
  17.     /// </summary>  
  18.     public class Impersonal  
  19.     {  
  20.         [DllImport("advapi32.dll", SetLastError = true)]  
  21.         public extern static bool LogonUser(String lpszUsername, String lpszDomain,  
  22.             String lpszPassword, int dwLogonType,  
  23.             int dwLogonProvider, ref IntPtr phToken);  
  24.   
  25.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]  
  26.         public extern static bool CloseHandle(IntPtr handle);  
  27.   
  28.         [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  29.         public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,  
  30.             int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);  
  31.   
  32.         const int LOGON32_PROVIDER_DEFAULT = 0;  
  33.         const int LOGON32_LOGON_INTERACTIVE = 2;  
  34.         const int SecurityImpersonation = 2;  
  35.         private IntPtr tokenHandle;  
  36.         private IntPtr dupeTokenHandle;  
  37.         private System.Security.Principal.WindowsImpersonationContext impersonatedUser;  
  38.         private string UserName;  
  39.         private string PWD;  
  40.   
  41.         public Impersonal(string username, string password)  
  42.         {  
  43.             tokenHandle = new IntPtr(0);  
  44.             dupeTokenHandle = new IntPtr(0);  
  45.             UserName = username;  
  46.             PWD = password;  
  47.         }  
  48.           
  49.         /// <summary>  
  50.         /// 开始模拟  
  51.         /// </summary>  
  52.         public void StartImpersonate()  
  53.         {  
  54.             string domainName = string.Empty;  
  55.             string userName = string.Empty;  
  56.               
  57.               
  58.             if (!System.Text.RegularExpressions.Regex.IsMatch(UserName, @"^/w+[//]?/w+$"))  
  59.             {  
  60.                 throw new ApplicationException("非法的用户名");  
  61.             }  
  62.             string[] tmp = UserName.Split(new char[] { '//' });  
  63.             if (tmp.Length > 1)  
  64.             {  
  65.                 domainName = tmp[0];  
  66.                 userName = tmp[1];  
  67.             }  
  68.             else  
  69.             {  
  70.                 userName = tmp[0];  
  71.             }  
  72.             tokenHandle = IntPtr.Zero;  
  73.             dupeTokenHandle = IntPtr.Zero;  
  74.             bool returnValue = LogonUser(userName, domainName, PWD,  
  75.                 LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,  
  76.                 ref tokenHandle);  
  77.             if (!returnValue)  
  78.             {  
  79.                 throw new ApplicationException("取Handle出错了!");  
  80.             }  
  81.   
  82.             //Console.WriteLine("当前用户是: "  
  83.             //    + WindowsIdentity.GetCurrent().Name);  
  84.   
  85.             bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);  
  86.             if (!retVal)  
  87.             {  
  88.                 CloseHandle(tokenHandle);  
  89.                 throw new ApplicationException("复制Handle出错了!");  
  90.             }  
  91.             System.Security.Principal.WindowsIdentity newId = new System.Security.Principal.WindowsIdentity(dupeTokenHandle);  
  92.             impersonatedUser = newId.Impersonate();  
  93.   
  94.         }  
  95.         /// <summary>  
  96.         /// 取消模拟  
  97.         /// </summary>  
  98.         public void StopImpersonate()  
  99.         {  
  100.             if (impersonatedUser != null)  
  101.                 impersonatedUser.Undo();  
  102.             if (tokenHandle != IntPtr.Zero)  
  103.                 CloseHandle(tokenHandle);  
  104.             if (dupeTokenHandle != IntPtr.Zero)  
  105.                 CloseHandle(dupeTokenHandle);  
  106.         }  
  107.     }  
  108. }  

 

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

使用方法
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"); 

http://blog.csdn.net/jiangxinyu/article/details/5410718

posted @ 2016-08-26 18:55  findumars  Views(1520)  Comments(0Edit  收藏  举报