您的位置:首页 > 其它

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

2010-03-24 10:15 399 查看
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"); 
 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string security iis class null c