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

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

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

使用方法
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.****.net/jiangxinyu/article/details/5410718