在这段代码中,我希望禁止 ReadFileSystem 方法在文件系统上声明权限。
我预计这会抛出 fileIo.Assert(),但事实并非如此。为什么?
using System.Security.Permissions;
static void Main(string[] args)
{
var fileIo = new FileIOPermission(PermissionState.Unrestricted);
var secuPerm = new SecurityPermission(SecurityPermissionFlag.Assertion);
PermissionSet set = new PermissionSet(PermissionState.Unrestricted);
set.AddPermission(fileIo);
set.AddPermission(secuPerm);
set.Deny();
ReadFileSystem();
Console.Read();
}
private static void ReadFileSystem()
{
var fileIo = newFileIOPermission(PermissionState.Unrestricted);
fileIo.Assert();
DirectoryInfo dir = new DirectoryInfo("C:/");
dir.GetDirectories();
}
更新
CAS 上的精彩链接:http: //blogs.msdn.com/shawnfa/archive/2004/08/25/220458.aspx