【发布时间】:2015-06-26 04:36:18
【问题描述】:
我在 MSDN 上阅读了有关代表的信息,看到一行写着
"注意: 委托在调用者的安全权限下运行,而不是在声明者的权限下运行"
这是什么意思?
【问题讨论】:
-
不确定......但我认为这意味着:假设 Windows 声明了一个供管理员使用的方法。现在您将方法称为甚至是客人。如果它允许来宾用户执行管理方法会有点糟糕
标签: c# permissions delegates
我在 MSDN 上阅读了有关代表的信息,看到一行写着
"注意: 委托在调用者的安全权限下运行,而不是在声明者的权限下运行"
这是什么意思?
【问题讨论】:
标签: c# permissions delegates
假设问题是关于 Windows 权限,而不是 .Net 代码访问安全 (CAS)。
无论您创建委托(即框管理员)时在什么帐户代码下运行 Windows 权限都将在实际调用时计算 - 这可能与创建时的不同。
假设您运行的代码会模拟帐户(Windows 用户)来访问某些文件:
// run under "account1" - has access to c:\myFile.txt
// current Environment.UserName = "account2"
Func<string,string> readAllFile = fileName => File.ReadAllText(fileName);
// start impersonation of account2 - has access to c:\otherFile.txt,
// but not c:\myFile.txt
ImpersonateAccount("account2", readAllFile);
....
...ImpersonateAccout(string name, Func<string,string> readAllFile)
{
// .... impersonation code omitted
// current Environment.UserName = "account2"
var text1 = readAllFile(@"c:\otherFile.txt"); // success
var text2 = readAllFile(@"c:\myFile.txt"); // failure
....
在上面的示例readAllFile 中,当代码在account1 下运行时创建,但它没有“捕获”该帐户的权限,因此后来的委托无法读取“account2”没有权限的c:\myFile.txt。
请注意,委托“捕获”C# 级别的上下文(如局部变量),这可能会带来其他类型的上下文也被捕获的假设。 Window 安全上下文和 .Net 执行上下文(例如当前线程的文化)并非如此。
【讨论】: