【问题标题】:Caller's Permission Vs Declarer's Permission调用者的许可与声明者的许可
【发布时间】:2015-06-26 04:36:18
【问题描述】:

我在 MSDN 上阅读了有关代表的信息,看到一行写着

"注意: 委托在调用者的安全权限下运行,而不是在声明者的权限下运行"

这是什么意思?

【问题讨论】:

标签: c# permissions delegates


【解决方案1】:

假设问题是关于 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 执行上下文(例如当前线程的文化)并非如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多