应用框架的设计与实现——.NET平台(10 授权服务.CodeAccessSecurityAttribute)
以下内容转载自: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()方法。


[AttributeUsage(AttributeTargets.Assembly |
AttributeTargets.Class |
AttributeTargets.Struct |
AttributeTargets.Constructor |
AttributeTargets.Method,
AllowMultiple=true, Inherited=false)]
public abstract class CodeAccessSecurityAttribute : SecurityAttribute
{
// Constructors.
internal CodeAccessSecurityAttribute()
: base()
{
// Nothing to do here.
}
public CodeAccessSecurityAttribute(SecurityAction action)
: base(action)
{
// Nothing to do here.
}
}; // class CodeAccessSecurityAttribute

public interface IPermission : ISecurityEncodable
{
IPermission Copy();
IPermission Intersect(IPermission target);
IPermission Union(IPermission target);
bool IsSubsetOf(IPermission target);
[DynamicSecurityMethodAttribute()]
void Demand();
}

public abstract class CodeAccessPermission : IPermission
{
// Constructor.
protected CodeAccessPermission() {}
// Assert permissions for the caller.
public void Assert()
{
if(!ClrSecurity.Assert(this, 1))
{
throw new SecurityException
(_("Exception_SecurityNotGranted"));
}
}
// Deny permissions to the caller.
public void Deny()
{
ClrSecurity.Deny(this, 1);
}
// Convert this object into a string.
public override String ToString()
{
return ToXml().ToString();
}
// Convert an XML value into a permissions value.
public abstract void FromXml(SecurityElement elem);
// Convert this permissions object into an XML value.
public abstract SecurityElement ToXml();
// Implement the IPermission interface.
public abstract IPermission Copy();
public void Demand()
{
if(!ClrSecurity.Demand(this, 1))
{
throw new SecurityException
(_("Exception_SecurityNotGranted"));
}
}
public abstract IPermission Intersect(IPermission target);
public abstract bool IsSubsetOf(IPermission target);
public virtual IPermission Union(IPermission target)
{
return null;
}
}; // class CodeAccessPermission
应用框架的设计与实现——.NET平台(10 授权服务.CodeAccessSecurityAttribute)的更多相关文章
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 9.插件引擎设计
目 录 第九章 插件引擎设计... 2 9.1 框架的契约-接口... 2 9.2 插件的雏形-抽象类... 3 9.3 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》-1.通讯框架介绍
[连载]<C#通讯(串口和网络)框架的设计与实现>- 0.前言 目 录 第一章 通讯框架介绍... 2 1.1 通讯的本质... 2 1 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》-2.框架的总体设计
目 录 C#通讯(串口和网络)框架的设计与实现... 1 (SuperIO)- 框架的总体设计... 1 第二章 框架总体的设计... 2 2.1 ...
- Java日志系统框架的设计与实现
推荐一篇好的文章介绍java日志系统框架的设计的文章:http://soft.chinabyte.com/database/438/11321938.shtml 文章内容总结: 日志系统对跟踪调试.程 ...
- 玩转OneNET物联网平台之MQTT服务⑤ —— OneNet智能灯+MVP框架
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲
Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲 Java生鲜电商平台: 微服务是当前非常流行的技术框架,通过服务的小型化.原子化以及分布式架构的弹性伸缩和高可用性, ...
- 《手把手教你》系列基础篇(九十六)-java+ selenium自动化测试-框架之设计篇-跨浏览器(详解教程)
1.简介 从这一篇开始介绍和分享Java+Selenium+POM的简单自动化测试框架设计.第一个设计点,就是支持跨浏览器测试. 宏哥自己认为的支持跨浏览器测试就是:同一个测试用例,支持用不同浏览器去 ...
- Mybatis原理分析之二:框架整体设计
1.引言 本文主要讲解Mybatis的整体程序设计,理清楚框架的主要脉络.后面文章我们再详细讲解各个组件. 2.整体设计 2.1 总体流程 (1)加载配置并初始化 触发条件:加载配置文件 ...
- activiti5.13 框架 数据库设计说明书
转载自:http://www.cnblogs.com/llzgzljl/p/3356108.html activiti5.13 框架 数据库设计说明书 1.结构设计 1.1. 逻辑结构设计 Ac ...
随机推荐
- safari渲染Transition动画不流畅问题
用css3的transition过渡来做页面动画的时候,发现在chrome和ff流畅,在safari 不流畅: 度娘找到了淘宝UED的一个类似解决方案,动画就流畅了. 测试环境: win7 32bit ...
- Python文档
详细的为代码编写文档,这其实是写好代码的重要部分. 常见编写代码的陷阱: 1.别忘了冒号.一定要记住在复合语句首行末未输入":" 2.从第一行开始.要确定顶层(无嵌套)程序代码从第 ...
- Firefox 23中的新特性(新陷阱)
话说有一天突然发现我们的网站页面上的JQuery功能都失效了,Firebug中显示如下的错误 Blocked loading mixed active content "http://xxx ...
- Framework7--Test
<!doctype html> <html> <head> <title>{{title}}</title> <meta charse ...
- java异常和spring事务注解
http://www.techferry.com/articles/spring-annotations.html http://www.oschina.net/question/2367675_23 ...
- ssh搭建后的简化
关于ssh如何搭建还有不懂得朋友可以参考以下网址:http://www.cnblogs.com/LarryBlogger/p/5841446.html 在这里我就不重复再讲了! ssh搭建后的简化 简 ...
- 使用 IntraWeb (41) - 数据控件速查
TIWDBCheckBox 所在单元及继承链: IWDBStdCtrls.TIWDBCheckBox 主要成员: property AutoEditable: Boolean //根据 DataSou ...
- 程序员遇到Bug时的25个反应
开发应用程序是一个非常有压力的工作.没有人是完美的,因此在这个行业中,代码中出现bug是相当普遍的现象.面对bug,一些程序员会生气,会沮丧,会心烦意乱,甚至会灰心丧气,而另一些程序员会依然保持冷静沉 ...
- [python] 线程池
特别感谢simomo 什么是线程池? 诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一 ...
- 【转】[caffe]深度学习之图像分类模型AlexNet解读
[caffe]深度学习之图像分类模型AlexNet解读 原文地址:http://blog.csdn.net/sunbaigui/article/details/39938097 本文章已收录于: ...