zoukankan      html  css  js  c++  java
  • 基于角色的安全性编程入门示例

    基于角色的安全性编程入门示例

    能更好地理解.NET基于角色的安全性,从一个最简单实例导入角色应用。首先创建一个简单控制台程序,如代码清单1-1所示。

    代码清单1-1  未添加角色验证程序

    using System;

    namespace角色实例

    {

        classProgram

        {

            static void Main(string[] args)

            {

                OutHello();

                Console.Read();

            }

            staticvoid OutHello()

            {

                Console.WriteLine("hello world!");

            }

        }

    }

    如代码清单1-1,这个程序只有两个方法:Main方法和OutHello方法,OutHello方法输出“hello world!”,在Main方法中调用OutHello方法。运行程序会打印字符串“hello world!”。下面对这个简单程序做略微修改,为它添加一些声明,如代码清单1-2所示。

    代码清单1-2  运行主体和主体权限要求主体不一致代码

    using System;

    using System.Security.Permissions;

    using System.Security.Principal;

    using System.Threading;

    namespace 角色实例

    {

        class Program

        {

            static void Main(string[] args)

            {

                Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetAnonymous());

               try

                {

                    OutHello();

                }

                catch(SecurityException e)

                {

                    Console.WriteLine(e.Message);

                }

                Console.Read();

            }

            [PrincipalPermission(SecurityAction.Demand,Role="Administrators")]

            static void OutHello()

            {

                Console.WriteLine("hello world!");

            }

        }

    }

    在代码清单1-2中,主要有三处变化:

    1)        新添了三个名称空间的引用,分别为System.Security.PermissionsSystem.Security.PrincipalSystem.Threading。通过第4章的讲解,我们对System.Security.Permissions名称空间已经很熟悉了,这里要使用其中的主体权限(PrincipalPermission)。System.Security.Principal是包含有关主体操作内容的名称空间。System.Threading是有关线程操作的名称空间。

    2)        对于OutHello方法,声明了PrincipalPermission权限,要求Administrators组成员才有权限执行此方法。

    3)        对于Main方法,使用以下语句设置当前线程的主体为匿名用户:

    Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetAnonymous())

    下面来看代码清单1-2运行结果,如图1-1所示。

    image

    1-1  代码清单1-2运行结果

    1-1表明,Main方法在调用OutHello方法时引发SecurityException 异常。因为Main方法设置主体为匿名用户,而OutHello方法要求Administrators组用户。下面把Main方法执行线程设置为当前Administrators用户,再看看运行结果。修改后Main方法如代码清单1-3所示。

    代码清单1-3  运行主体和主体权限要求主体一致代码

      staticvoid Main(string[] args)

            {

                Thread.CurrentPrincipal = new System.Security.Principal.WindowsPrincipal(WindowsIdentity.GetCurrent());

                try

                {

                    OutHello();

                }

                catch (SecurityException e)

                {

                    Console.WriteLine(e.Message);

                }

                Console.Read();

            }

    代码清单1-3设置Main方法运行线程主体为当前用户,本书测试当前用户为Administrator(最高权限用户,属于Administrators),此时运行结果如图1-2所示。

    image

    1-2  代码清单1-3运行结果

    通过代码清单1-1、代码清单1-2和代码请单1-3对比,可以粗略地解主体验证作用。在前面代码中采用声明式安全检查方法,同其权限一样,这里仍然可以使用命令式安全检查方法来达到同样效果,对于声明式和命令式安全检查还不太熟悉读者可以回顾第4相关内容。修改后代码如代码清单1-4所示。

    代码清单1-4  使用命令式安全检查来指定主体权限

    namespace 角色实例

    {

        class Program

        {

            static void Main(string[] args)

            {

                Thread.CurrentPrincipal = new System.Security.Principal.WindowsPrincipal(WindowsIdentity.GetCurrent());

                try

                {

                    OutHello();

                }

                catch (SecurityException e)

                {

                    Console.WriteLine(e.Message);

                }

                Console.Read();

            }

            static void OutHello()

            {

                new PrincipalPermission("", "Administrators").Demand();

                Console.WriteLine("hello world!");

            }

        }

    }

    代码清单1-4中,通过创建PrincipalPermission实例方式来标识当前方法主体权限请求。使用PrincipalPermission类比使用PrincipalPermissioAtrribute进行声明方法更加灵活,可以充分运用它多个重载方法。下面继续修改Main方法,使用标识对象,如代码清单1-5所示。

    代码清单1-5  使用标识

    namespace 角色实例

    {

        class Program

        {

            static void Main(string[] args)

            {

                GenericIdentity identity = new GenericIdentity("xuanhun");

                string[] roles = new string[] { "Administrators" };

                  GenericPrincipal principal = new GenericPrincipal(identity, roles);

                  AppDomain.CurrentDomain.SetThreadPrincipal(principal);

                OutHello(); 

     Console.Read();

    }

        static void OutHello()

            {

                new PrincipalPermission("xx", "Administrators").Demand();

                Console.WriteLine("hello world!");    }

    }

    在代码清单1-5Main方法中,首先创建用户自定义标识对象identity,然后创建角色数组roles,并指定主体拥有角色数组,最后设定当前应用程序域主体。在OutHello方法中,要求标识名称为xx;而在Main方法中指定标识为xuanhun,运行程序将引发异常,如图1-3所示。

    image

    1-3  标识不一致运行结果

    到目前为止,们体验.NET角色验证,在感性上有简单认识。更详细内容,可以参考《.NET 安全揭秘》第五章。


    作者:玄魂
    出处:http://www.cnblogs.com/xuanhun/
    原文链接:http://www.cnblogs.com/xuanhun/ 更多内容,请访问我的个人站点 对编程,安全感兴趣的,加qq群:hacking-1群:303242737,hacking-2群:147098303,nw.js,electron交流群 313717550。
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    关注我:关注玄魂的微信公众号

  • 相关阅读:
    模块入门–搜索
    [hadoop源码阅读][2]package结构
    [hadoop源码阅读][8]datanodeDataStorage
    [hadoop源码阅读][4]org.apache.hadoop.io
    [hadoop源码阅读][6]org.apache.hadoop.ipcprotocol和心跳分析
    [hadoop源码阅读][1]源码目录结构
    [hadoop源码阅读][4]org.apache.hadoop.io.compress系列3使用压缩
    [hadoop源码阅读][3]新旧api区别
    [hadoop源码阅读][6]org.apache.hadoop.ipcipc总体结构和RPC
    [hadoop源码阅读][8]datanodeFSDataset
  • 原文地址:https://www.cnblogs.com/xuanhun/p/2559431.html
Copyright © 2011-2022 走看看