[C#] C#如何通过权限类和权限属性来修改安全权限 →→→→→进入此内容的聊天室

来自 , 2021-01-25, 写在 C#, 查看 179 次.
URL http://www.code666.cn/view/7c5845ce
  1. using System;
  2. using System.Security;
  3. using System.Security.Permissions;
  4. using System.Runtime.InteropServices;
  5.  
  6. class NativeMethods
  7. {
  8.     // 这是对非托管代码的调用。执行此方法需要
  9.     // UnmanagedCode 安全权限。如果没有此权限,
  10.     // 则调用此方法的尝试将引发 SecurityException:
  11.     [DllImport("msvcrt.dll")]
  12.     public static extern int puts(string str);
  13.     [DllImport("msvcrt.dll")]
  14.     internal static extern int _flushall();
  15. }
  16.  
  17. class MainClass
  18. {
  19.     private static void CallUnmanagedCodeWithoutPermission()
  20.     {
  21.         // 创建安全权限对象以描述
  22.         // UnmanagedCode 权限:
  23.         SecurityPermission perm =
  24.            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  25.  
  26.         // 拒绝当前权限集中的 UnmanagedCode。
  27.         // 在此方法返回之前,在此线程上调用的任何方法
  28.         // 都不允许访问非托管代码。
  29.         // 即使 CallUnmanagedCodeWithPermission 方法
  30.         // 是从已经为非托管代码调用了
  31.         // Assert 的堆栈帧调用的,也无法调用本机
  32.         // 代码。由于此处使用了 Deny,因此权限
  33.         // 被覆盖。
  34.         perm.Deny();
  35.  
  36.         try
  37.         {
  38.             Console.WriteLine("Attempting to call unmanaged code without permission.");
  39.             NativeMethods.puts("Hello World!");
  40.             NativeMethods._flushall();
  41.             Console.WriteLine("Called unmanaged code without permission. Whoops!");
  42.         }
  43.         catch (SecurityException)
  44.         {
  45.             Console.WriteLine("Caught Security Exception attempting to call unmanaged code.");
  46.         }
  47.     }
  48.  
  49.     private static void CallUnmanagedCodeWithPermission()
  50.     {
  51.         // 创建安全权限对象来描述
  52.         // UnmanagedCode 权限:
  53.         SecurityPermission perm =
  54.            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  55.  
  56.         // 检查您是否具有访问非托管代码的权限。
  57.         // 如果您没有访问非托管代码的权限,则
  58.         // 此调用将引发 SecurityException。
  59.         // 即使 CallUnmanagedCodeWithPermission 方法
  60.         // 是从已经为非托管代码调用了 Assert 的堆栈帧调用的,
  61.         // 也不能调用本机
  62.         // 代码。由于此处使用了 Deny,因此权限被
  63.         // 覆盖。
  64.         perm.Assert();
  65.  
  66.         try
  67.         {
  68.             Console.WriteLine("Attempting to call unmanaged code with permission.");
  69.             NativeMethods.puts("Hello World!");
  70.             NativeMethods._flushall();
  71.             Console.WriteLine("Called unmanaged code with permission.");
  72.         }
  73.         catch (SecurityException)
  74.         {
  75.             Console.WriteLine("Caught Security Exception attempting to call unmanaged code. Whoops!");
  76.         }
  77.     }
  78.  
  79.     public static void Main()
  80.     {
  81.         // 该方法本身将为非托管代码调用安全权限 Deny,
  82.         // 这会重写此堆栈帧中的 Assert
  83.         // 权限。
  84.         SecurityPermission perm = new
  85.             SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
  86.         perm.Assert();
  87.         CallUnmanagedCodeWithoutPermission();
  88.  
  89.         // 该方法本身将为非托管代码调用安全权限 Assert,
  90.         // 这会重写此堆栈帧中的 Deny
  91.         // 权限。
  92.         perm.Deny();
  93.         CallUnmanagedCodeWithPermission();
  94.     }
  95. }
  96.  
  97.  
  98.  
  99.  
  100. //csharp/4923

回复 "C#如何通过权限类和权限属性来修改安全权限"

这儿你可以回复上面这条便签

captcha