.NET安全揭秘--第5章 基于角色的安全性

1.匿名用户无权限访问

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

namespace SafeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetAnonymous());
            try
            {
                OutHello();
            }
            catch ( Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        //define the role  of administrators  have permission
        [PrincipalPermission(SecurityAction.Demand,Role="Administrators")]
        static void OutHello()
        {
            Console.WriteLine("Hello world");
        }
    }
}


2. 有权限访问的用户

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

namespace SafeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
           // Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetAnonymous());
            Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            try
            {
                OutHello();
            }
            catch ( Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        //define the role  of administrators  have permission
        [PrincipalPermission(SecurityAction.Demand,Role="Users")]
        static void OutHello()
        {
            Console.WriteLine("Hello world");
        }
    }
}

3.主体和标识

   1)主体对象表示代码运行时所在的安全上下文,实现基于角色的安全性的应用程序将基于与主体对象关联的角色来授权

       .NET 提供 GenericPrincipal对象和 WindowsPrincipal对象。还可以定义自己的自定义主体类

 

        static void Main(string[] args)
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal wp = new WindowsPrincipal(id);
            if (wp.IsInRole(0x220))
            {
                Console.WriteLine("当前用户是管理员账号");
            }
            Console.ReadLine();
        }

   自定义主体

 

        static void Main(string[] args)
        {
            GenericIdentity identity = new GenericIdentity("test");
            string[] roles = new string[] { "Administrators" };
            GenericPrincipal principal = new GenericPrincipal(identity, roles);
            AppDomain.CurrentDomain.SetThreadPrincipal(principal);
            OutHello();
            Console.ReadLine();
        }


2)标识对象--封装有关正在验证的用户或实体信息

4.安全检查

     安全实施要依靠安全检查,.NET对它提供的所有安全权限都提供了命令式和声明式两种检查方式。也可直接访问主体对象进行if--else的判断

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值