为一个单一的方法管理权限 [英] Admin rights for a single method

查看:116
本文介绍了为一个单一的方法管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能需要一个单一的方法管理员权限?

事情是这样的:

  [RequireAdminRightsForThisMethod()]私人无效TheMethod(){    // 做一点事}


解决方案

您可以添加<一个href=\"http://msdn.microsoft.com/en-us/library/system.security.permissions.principalpermissionattribute.aspx\"><$c$c>PrincipalPermission属性来你的方法,要求其执行管理权限:

  [的PrincipalPermission(SecurityAction.Demand,角色= @BUILTIN \\管理员)]
公共无效的MyMethod()
{
}

此进行更详细的在下面的文章中描述:


  

安全原则和本地管理员权限在C#.NET


如果你正在寻找一种方式来提升已经存在的过程中,我怀疑这是可能的,管理员权限是在启动时的进程级给一个过程(见本相关的<一个href=\"http://stackoverflow.com/questions/573086/how-to-elevate-privileges-only-when-required\">question).你将不得不运行应用程序以管理员身份来获得所需的行为。

不过,也有一些技巧,可以让你做你想要什么,但被警告,这可能打开严重的安全隐患。请参见下面的线程在MSDN论坛:


  

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/windowscompatibility/thread/c2cbac82-de4c-4058-8082-74507fba1492\">Launching而不提示管理员MyElevatedCom服务器从标准用户credentialls


更新(从评论)

看来,如果更新需要提升你的应用程序更新最好由一个单独的进程(或者其他可执行文件,或者你调用一个命令行开关应用程序)来完成。对于独立的过程可以按如下要求海拔:

  VAR PSI =新的ProcessStartInfo();
psi.FileName =路径的Update.exe
psi.Arguments =论点的Update.exe
psi.Verb =运行方式;VAR工艺=新工艺();
process.StartInfo =磅;
的Process.Start();
process.WaitForExit();

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(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

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:

Launching MyElevatedCom Server without prompting Administrator credentialls from Standard User

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();

这篇关于为一个单一的方法管理权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆