关于如何进行权限分配

根据数据库中权限来判断控件是否可以使用 以往这个做的很乱

下面我找到了个关于一般标识的使用,可以很清晰的完成权限分配,但似乎只适合于Winform,现在先让我们了解下:
使用基于角色的安全性:1.身份验证2.授权3.标识

.NET Framework提供了三种标识对象:

windows标识,下面是一个windows标识的例子

using  System;
using  System.Threading;
using  System.Security.Principal;
public   class  class1
{
    
public static int Main(string[] args)
    
{
        
        
//返回当前windows用户
        WindowsIdentity MyIdentity=WindowsIdentity.GetCurrent();
        
//允许访问代码检查windows用户的windows组成员身份
        WindowsPrincipal MyPrincipal=new WindowsPrincipal(MyIdentity);
        
        
//收集标识和主体信息

        
//主体值

        
//获取当前用户的名称
        string Name=MyPrincipal.Identity.Name;
        
//获取身份验证的类型
        string Type=MyPrincipal.Identity.AuthenticationType;
        
//获取一个值,该值指示是否验证了用户
        string Auth=MyPrincipal.Identity.IsAuthenticated.ToString();
        
//判断所处的角色可以用IsinRole
        
//MyPrincipal.IsInRole()

        
//标识值
        
        
//获取windows用户登陆名
        string IdentName=MyIdentity.Name;
        
//获取身份验证的类型
        string IdentType=MyIdentity.AuthenticationType;
        
//获取一个值,该值指示是否验证了用户
        string IdentIsAuth=MyIdentity.IsAuthenticated.ToString();
        
//获取一个值,该值指示系统是否将用户帐户标识为匿名帐户
        string IsAnon=MyIdentity.IsAnonymous.ToString();
        
//系统是否windows用户标识为guest用户
        string IsG=MyIdentity.IsGuest.ToString();
        
//系统是否将windows用户标识为System用户
        string IsSys=MyIdentity.IsSystem.ToString();
        
//获取用户windows帐户标记
        string Token=MyIdentity.Token.ToString();
        
//....
        return 0;


    }

}

一般标识,下面是一个一般标识的例子,一般是用这种方式来完成对数据库当中的一些权限进行比较完成权限分配

 

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;
using  System.Security.Principal;

namespace  关于权限分配
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button button1;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            
string user="ding";
            
string[] roles={"manager","employee"};
            GenericIdentity gi
=new GenericIdentity(user);
            GenericPrincipal gp
=new GenericPrincipal(gi,roles);
            System.Threading.Thread.CurrentPrincipal
=gp;
        }


        
private void button1_Click(object sender, System.EventArgs e)
        
{
        
            
if(System.Threading.Thread.CurrentPrincipal.IsInRole("manager"))
            
{
                MessageBox.Show(
"this is a success Test!");
                Form2 f2
=new Form2();
                f2.ShowDialog();
            }

            
else
            
{
                MessageBox.Show(
"this is a failed Test!");
            }

        }

    }

}

 

自定义标识,一般很少用,也没去查

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值