C#中的安全策略

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运行
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Runtime.InteropServices;
using  System.Reflection;
using  System.Security;
using  System.Security.Permissions;

namespace  Essence
{
    
public class MyClass
    
{
        [DllImport(
"user32.dll")]
        
public static extern int MessageBox(uint hWnd, string lpText, string lpCaption, uint uType);
    }


    
public  class MY
    
{

        
public static void CallUnmanagedCodeWithoutPermission()
        
{
            SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            perm.Deny();
            
try
            
{
                  MyClass.MessageBox(
0"Unmanaged code is executed with Permission!""Security Policy Demo"0);
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message.ToString());
            }

        }

  
    }

    
class Program
    
{
        
static   void Main(string[] args)
        
{
            MY.CallUnmanagedCodeWithoutPermission();
        }

    }

}


运行结果如下:抛出异常!


    2.允许unmanaged code 运行
    代码如下:
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Runtime.InteropServices;
using  System.Reflection;
using  System.Security;
using  System.Security.Permissions;

namespace  Essence
{
    
public class MyClass
    
{
        [DllImport(
"user32.dll")]
        
public static extern int MessageBox(uint hWnd, string lpText, string lpCaption, uint uType);
    }


    
public  class MY
    
{

        
public static void CallUnmanagedCodeWithoutPermission()
        
{
            SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            perm.Assert();
            
try
            
{
                  MyClass.MessageBox(
0"Unmanaged code is executed with Permission!""Security Policy Demo"0);
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message.ToString());
            }

        }

  
    }

    
class Program
    
{
        
static   void Main(string[] args)
        
{
            MY.CallUnmanagedCodeWithoutPermission();
        }

    }

}

运行结果:运行unmanaged code, popup a window


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

class  NativeMethods
{
    
// 这是对非托管代码的调用。执行此方法需要
    
// nmanagedCode 安全权限。如果没有此权限,
    
// 则调用此方法的尝试将引发 SecurityException:
    /**//* 注意:SuppressUnmanagedCodeSecurityAttribute 禁止
       在运行时检查 UnmanagedCode 权限。一定要小心!
*/

    [SuppressUnmanagedCodeSecurityAttribute()]
    [DllImport(
"msvcrt.dll")]
    
internal static extern int puts(string str);
    [SuppressUnmanagedCodeSecurityAttribute()]
    [DllImport(
"msvcrt.dll")]
    
internal static extern int _flushall();
}

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

转载于:https://www.cnblogs.com/Winston/archive/2008/05/26/1207661.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值