1.安全性
    .Net的安全性包括代码和数据不被错误的使用或者被其他的程序破坏,这种安全机制是通过加强对托管代码的限制和保护来实施的(This security mechanism is implemented by strengthing restriction and protection for the managed code)。 .Net 程序需要请求他们操作, 有管理员设定的安全策略进行判断,如果有足够的权限,代码将被执行;否则如果权限不够,将抛出SecurityException,代码不被执行。权限是通过数据签名产生的,数字签名包括了代码从哪里来,语言,public key token等信息(permission is generated through digital signature or strong name, digital signature includes assembly's information, such as from which corperation,language,version, public key token).
    All of these concerns one definiton: Permission

2. 安全策略Security policy
    安全策略的执行是.Net托管代码安全的保证。
    我们可以通过修改对托管或者非托管代码的安全策略,来实现对程序的安全访问。
    有关安全策略的所有library位于:    
    System.Security以及System.Security.Permission;


3.例子:对非托管代码的授权/禁止运行示例
     1.禁止unmanaged code运行
C#中的安全策略using System;
C#中的安全策略
using System.Collections.Generic;
C#中的安全策略
using System.Linq;
C#中的安全策略
using System.Text;
C#中的安全策略
using System.Runtime.InteropServices;
C#中的安全策略
using System.Reflection;
C#中的安全策略
using System.Security;
C#中的安全策略
using System.Security.Permissions;
C#中的安全策略
C#中的安全策略
namespace Essence

运行结果如下:抛出异常!
C#中的安全策略

    2.允许unmanaged code 运行
    代码如下:
C#中的安全策略using System;
C#中的安全策略
using System.Collections.Generic;
C#中的安全策略
using System.Linq;
C#中的安全策略
using System.Text;
C#中的安全策略
using System.Runtime.InteropServices;
C#中的安全策略
using System.Reflection;
C#中的安全策略
using System.Security;
C#中的安全策略
using System.Security.Permissions;
C#中的安全策略
C#中的安全策略
namespace Essence
运行结果:运行unmanaged code, popup a window
C#中的安全策略

4.安全性的进一步讨论:
        目前为止,我们知道了.Net Framework提供了良好的安全验证功能。对于managed code,我们可以采用strong name来对Assembly获得digital signature的方法来进行安全性的验证,防止DLL Hell的产生。 同时,对于unmanaged code,可以利用.Net 中的安全验证机制使得unmanaged code不允许运行。
        简言之,.Net安全系统防止了从网上下载有恶意的程序来保护计算机系统。但是,即使不触发安全异常,这些安全检查也是要付出代价的。那么也可以在调用unmanged code的时候,使用特性SuppressUnmanagedCodeSecurity来跳过安全性检查(前提是你对unmanaged code 足够信任)。
例如:
C#中的安全策略using System;
C#中的安全策略
using System.Security;
C#中的安全策略
using System.Security.Permissions;
C#中的安全策略
using System.Runtime.InteropServices;
C#中的安全策略
C#中的安全策略
class NativeMethods
}

这样,在client 调用NativeMethods时候,不论是否有SecurityPermission,都将不起作用,因为它会跳过安全性检查。

相关文章:

  • 2021-12-30
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
  • 2021-04-30
  • 2021-11-08
猜你喜欢
  • 2021-12-29
  • 2021-12-31
  • 2021-12-30
  • 2021-11-25
  • 2022-02-08
  • 2021-06-04
相关资源
相似解决方案