选择显示字体大小

学习一下.net framework 中有关安全的内容

     好容易在繁重的开发任务之余抽出点时间学习一些东西。发现机子里有几个关于 system.security 内容的示例,这一个命名空间以前还真是从来没用过,正好拿来学习一下。由于不是系统的学习,不好组织,想了想,就以示例来说明吧。 
  
    一、设定权限
  
  1[fileiopermission(securityaction.demand, write= "c:\\temp.txt")]
  2public class app : system.windows.forms.form
  3{
  4 //略
  5}
    fileiopermissionattribute 定义于 system.security.permissions 里。它继承于 securityattribute,在这个例子中,要求使用 app 类时必须具有对 c:\temp.txt 文件的写权限。
  
    .net framework 的文档中关于安全要求有这样一段话:“若要确保只有被授予了指定权限的调用方才能够调用您的代码,可以声明方式或强制方式要求您的代码的调用方拥有特定的权限或权限集。要求使运行库执行安全检查,从而对调用代码实施限制。在安全检查过程中,运行库遍历调用堆栈,检查堆栈中每个调用方的权限,然后确定是否已将要求的权限授予每个调用方。如果发现某个调用方没有要求的权限,则安全检查失败,并引发 securityexception。”
  
    例子中,权限是以声明的方式出现的。securityaction.demand 可以作用于类或方法,在这里是作用于类上。write 是 fileiopermission 的属性之一,其它常用属性还有 read、append、all 等等。
  
    securityaction 枚举中还有一些值是作用于 assembly 上的。比如以下的例子:
  
  [assembly:securitypermission(securityaction.requestminimum ,unmanagedcode=true)]
    securityaction.requestminimum 是请求运行的最小权限。这一行要求程序集允许调用非托管代码。
  
    除了声明方式外,还可以使用强制方式。如下的代码:
  
   1fileiopermission fileperm = new fileiopermission(fileiopermissionaccess.allaccess, "c:\\temp.txt");
   2try
   3{
   4 fileperm.demand();
   5
   6 // code to access file goes here
   7}
   8catch (securityexception excep)
   9{
  10 messagebox.show (excep.message);
  11 return;
  12}
  13
    二、用户角色管理
  
    用户及其角色的管理是在许多程序中都要使用到的。如今 asp.net 2.0 对于这方面有了大大增强,开发人员不需要很了解技术就可以做出很不错的应用。不过对于 windows form 应用程序来说,不少地方还需要程序员自己设定。
  
    假定我们已知晓了 username 以及它所属于的 roles,那么可以这样来设置当前线程的 principal:
  
  1genericidentity genident = new genericidentity(username);
  2genericprincipal genprin = new genericprincipal(genident, roles);
  3thread.currentprincipal = genprin;
  4
    随后我们有三种办法来进行用户角色验证。
  
    第一种方法是使用 genericprincipal.isinrole 方法:
  
  1genericprincipal currentprin = thread.currentprincipal as genericprincipal;
  2
  3if (currentprin != null && currentprin.isinrole("manager"))
  4{
  5 //略
  6}
  7
    第二种方法则是使用 principalpermission 类,类似于权限设定中的强制方式:
  
   1principalpermission prinperm = new principalpermission(null, "manager");
   2
   3try
   4{
   5 prinperm.demand();
   6
   7 //do something
   8}
   9catch
  10{
  11 //error handling
  12}
    第三种方式则类似于权限设定中的声明方式:
  
   1private void decpermbutton_click(object sender, system.eventargs e)
   2{
   3 try
   4 {
   5 performmanageraction();
   6 // do something
   7 }
   8 catch
   9 {
  10 // error handling
  11 }
  12}
  13
  14[principalpermission(securityaction.demand, role="manager")]
  15void performmanageraction()
  16{
  17}
    关于安全的另一个重要内容是加密。今天没空写了,改天再说。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons