您的位置:首页 > 博客中心 > 数据库 >

应用框架的设计与实现——.NET平台(10 授权服务.CodeAccessSecurityAttribute)

时间:2022-03-14 02:19

以下内容转载自:http://blog.csdn.net/dongma_yifei/article/details/1533124

 

CodeAccessSecurityAttribute 是 PrincipalPermissionAttribute 的父类,并且也是编译器和CLR都认识的一个框架类。

CodeAccessPermission 继承自 IPermission。


做权限验证工作的是 IPermission 接口的实现类,在CLR验证权限的时候会调用 IPermission 的 Demand() 方法。
IPermission 接口类的对象由 CodeAccessSecurityAttribute 实现类对象的 CreatePermission() 方法生成;
CreatePermission() 方法使用了工厂模式,用户可以在方法中返回一个实现了 IPermission 接口的类对象。

CodeAccessSecurityAttribute 和 CodeAccessPermission 都是抽象类,不能用来生成对象,用户必须定义自己的继承类来实现。


编译器发现标记了 CodeAccessSecurityAttribute 的方法时,有以下几个处理步骤:
1.编译器扫描源代码,找出 CodeAccessSecurityAttribute 类型的特性;
2.编译器创建特性对象,并用源代码中特性标签所指定的属性值给特性对象赋值;
3.编译器调用特性对象的 CreatePermission() 方法,创建一个权限对象,将特性信息传递给它,然后将对象返回给编译器;
4.编译器调用权限对象的 ToXml() 方法,获得一个SecurityElement对象;
5.编译器将 ToXml() 方法返回的对象转化为 XML 数据并存入应用的元数据。


在程序的执行期内,当CLR发现权限集信息后,它首先会使用其中的类型信息创建一个权限对象,然后CLR会调用权限对象的FromXml()方

法,同时传入方法元数据的XML数据,在权限对象属性都被赋值后,CLR将调用它的Demand()方法。
 

gxlsystem.com,布布扣
gxlsystem.com,布布扣
gxlsystem.com,布布扣[AttributeUsage(AttributeTargets.Assembly |
gxlsystem.com,布布扣                 AttributeTargets.Class |
gxlsystem.com,布布扣                 AttributeTargets.Struct |
gxlsystem.com,布布扣                 AttributeTargets.Constructor |
gxlsystem.com,布布扣                 AttributeTargets.Method,
gxlsystem.com,布布扣                 AllowMultiple=true, Inherited=false)]
gxlsystem.com,布布扣public abstract class CodeAccessSecurityAttribute : SecurityAttribute
gxlsystem.com,布布扣{
gxlsystem.com,布布扣    // Constructors.
gxlsystem.com,布布扣    internal CodeAccessSecurityAttribute()
gxlsystem.com,布布扣            : base()
gxlsystem.com,布布扣            {
gxlsystem.com,布布扣                // Nothing to do here.
gxlsystem.com,布布扣            }
gxlsystem.com,布布扣    public CodeAccessSecurityAttribute(SecurityAction action)
gxlsystem.com,布布扣            : base(action)
gxlsystem.com,布布扣            {
gxlsystem.com,布布扣                // Nothing to do here.
gxlsystem.com,布布扣            }
gxlsystem.com,布布扣
gxlsystem.com,布布扣}; // class CodeAccessSecurityAttribute
gxlsystem.com,布布扣
gxlsystem.com,布布扣
gxlsystem.com,布布扣public interface IPermission : ISecurityEncodable
gxlsystem.com,布布扣{
gxlsystem.com,布布扣    IPermission Copy();
gxlsystem.com,布布扣
gxlsystem.com,布布扣    IPermission Intersect(IPermission target);
gxlsystem.com,布布扣
gxlsystem.com,布布扣    IPermission Union(IPermission target);
gxlsystem.com,布布扣
gxlsystem.com,布布扣    bool IsSubsetOf(IPermission target);
gxlsystem.com,布布扣
gxlsystem.com,布布扣    [DynamicSecurityMethodAttribute()]
gxlsystem.com,布布扣    void Demand();
gxlsystem.com,布布扣}
gxlsystem.com,布布扣
gxlsystem.com,布布扣
gxlsystem.com,布布扣public abstract class CodeAccessPermission : IPermission
gxlsystem.com,布布扣{
gxlsystem.com,布布扣    // Constructor.
gxlsystem.com,布布扣    protected CodeAccessPermission() {}
gxlsystem.com,布布扣
gxlsystem.com,布布扣    // Assert permissions for the caller.
gxlsystem.com,布布扣    public void Assert()
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        if(!ClrSecurity.Assert(this, 1))
gxlsystem.com,布布扣        {
gxlsystem.com,布布扣            throw new SecurityException
gxlsystem.com,布布扣                (_("Exception_SecurityNotGranted"));
gxlsystem.com,布布扣        }
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣
gxlsystem.com,布布扣    // Deny permissions to the caller.
gxlsystem.com,布布扣    public void Deny()
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        ClrSecurity.Deny(this, 1);
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣
gxlsystem.com,布布扣    // Convert this object into a string.
gxlsystem.com,布布扣    public override String ToString()
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        return ToXml().ToString();
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣
gxlsystem.com,布布扣    // Convert an XML value into a permissions value.
gxlsystem.com,布布扣    public abstract void FromXml(SecurityElement elem);
gxlsystem.com,布布扣
gxlsystem.com,布布扣    // Convert this permissions object into an XML value.
gxlsystem.com,布布扣    public abstract SecurityElement ToXml();
gxlsystem.com,布布扣
gxlsystem.com,布布扣    // Implement the IPermission interface.
gxlsystem.com,布布扣    public abstract IPermission Copy();
gxlsystem.com,布布扣    public void Demand()
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        if(!ClrSecurity.Demand(this, 1))
gxlsystem.com,布布扣        {
gxlsystem.com,布布扣            throw new SecurityException
gxlsystem.com,布布扣                (_("Exception_SecurityNotGranted"));
gxlsystem.com,布布扣        }
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣    public abstract IPermission Intersect(IPermission target);
gxlsystem.com,布布扣    public abstract bool IsSubsetOf(IPermission target);
gxlsystem.com,布布扣    public virtual IPermission Union(IPermission target)
gxlsystem.com,布布扣    {
gxlsystem.com,布布扣        return null;
gxlsystem.com,布布扣    }
gxlsystem.com,布布扣}; // class CodeAccessPermission
gxlsystem.com,布布扣

热门排行

今日推荐

热门手游