让管理员权限Keeping administrator privileges

- 此内容更新于:2015-01-06
主题:

原文:

I wrote a WinForms C# app that needs administrator privileges to work and also needs to start at computer startup (with registry).

 RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
 reg.SetValue("My app", Application.ExecutablePath.ToString());

So I tried changing the manifest to requiredAdministrator and got an error about clickOnce, which I completely didn't understand. So I tried publishing the app and installing as administrator, but then when the application starts at startup it doesn't have the administrator privileges anymore.

Anyone knows how to get administrator privileges for good?

Damien_The_Unbeliever的回复:

(原文:It sounds like your program would be a massive focus for attacks if it ever became popular since you guarantee that there's code running with Admin privileges on the desktop of every logged in user. Can you not split your program into a service that's set to run with enough permissions/privileges it needs to do its job and then a non-admin program that runs for each user and communicates with the service to get the (overall) jobs done?)

Justin的回复:如果应用程序需要运行在启动然后你考虑过创建一个windows服务呢?

(原文:If your application needs to run at startup then have you considered creating a windows service instead?)

解决方案:
你可以尝试使用自升式,没有确定它将如何寻找自动起动程序(@peer一样回答)
原文:

You can try to use self-elevating, no sure how it will looks for auto-starting application (same as @peer answer)

// at application start
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);

// if not admin - elevate
if(!isAdmin)
{
    var info = new ProcessStartInfo(Application.ExecutablePath) { Verb = "runas" };
    Process.Start(info);
    return; // exit
}

// if we are here - application runs as admin
...
解决方案:
你可以去快捷键= = >属性>高级…= >检查作为管理员运行。 现在您已经配置了你的快捷与管理员权限启动应用程序。
原文:

You can go to the shortcut => Properties => Advanced ... => check Run as administrator.

Now you have configured your shortcut to start the application with administrator privileges.

解决方案:
你需要的是海拔请求许可。通常是通过清单,或者如果你只是一个过程,您就可以开始提升权限。这给了一个解释,这解释了如何嵌入清单。
原文:

What you need is permission elevation request. Usually it is done via manifest, or if you have just a process you can start it with elevated permissions. This gives an explanation, and this explains how to embed the manifest.