简体   繁体   English

单个方法的管理员权限

[英]Admin rights for a single method

Is it possible to require administrator rights for one single method? 是否可以为单个方法要求管理员权限?

Something like this: 像这样的东西:

[RequireAdminRightsForThisMethod()]

private void TheMethod(){

    // Do something

}

You can add a PrincipalPermission attribute to your method to demand administrative privileges for its execution: 您可以向方法添加PrincipalPermission属性,以请求其执行的管理权限:

[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
public void MyMethod()
{
}

This is described in more detail in the following article: 这在以下文章中有更详细的描述:

Security Principles and Local Admin Rights in C# .Net C#.Net中的安全原则和本地管理员权限

If you are looking for a way to elevate an already existing process I doubt that this is possible as administrator privileges are given on process-level to a process upon startup (see this related question ). 如果您正在寻找提升现有流程的方法,我怀疑这是可行的,因为在启动时会在流程级别为流程提供管理员权限(请参阅此相关问题 )。 You would have to run your application "as administrator" to get the desired behavior. 您必须“以管理员身份”运行您的应用程序才能获得所需的行为。

However, there are some tricks that might allow you to do what you want, but be warned that this might open up severe security risks. 但是,有一些技巧可能会让你做你想做的事情,但要注意这可能会带来严重的安全风险。 See the following thread in the MSDN forums: 请参阅MSDN论坛中的以下主题:

Launching MyElevatedCom Server without prompting Administrator credentialls from Standard User 在不提示标准用户的管理员凭据的情况下启动MyElevatedCom服务器

Update (from comment) 更新(来自评论)

It seems that if an update requires elevation your application update is best done by a separate process (either another executable, or your application called with a command line switch). 似乎如果更新需要提升,则应用程序更新最好由单独的进程(另一个可执行文件或使用命令行开关调用的应用程序)完成。 For that separate process you can request elevation as follows: 对于该单独的流程,您可以按如下方式请求提升:

var psi = new ProcessStartInfo();
psi.FileName = "path to update.exe";
psi.Arguments = "arguments for update.exe";
psi.Verb = "runas";

var process = new Process();
process.StartInfo = psi;
process.Start();   
process.WaitForExit();

方法可能需要管理权限才能运行,但在执行方法时无法自动提升为Admin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM