代码阅读总结之ASP.NET StartKit TimeTracker(角色权限)


最近开始看ASP.NET StartKit TimeTracker中代码,它是一个典型的项目追踪系统。
它比我前几天看的ASP.NET StartKit Commerce复杂了许多。
例如:在ASP.NET StartKit TimeTracker开始有明显的三层结构的设计。PL层,BLL层和DAL层。
同时开始在项目中引进了角色权限管理功能等等。

今天我们先讨论角色权限的实现问题。

让我们先看一角色权限设置的参考资料:
http://www.cnblogs.com/kwklover/archive/2004/06/29/19455.aspx

先说说大概步骤:
1.在用户成功登陆后,将用户的角色数据经过FormsAuthentication.Encrypt加密放到验证coolie
2.在Application_AuthenticateRequest事件中取回验证cookie(FormsAuthentication.FormsCookieName),再FormsAuthentication.Decrypt方法解密
3.创建IPrincipal对象并存在HttpContext.User中


ASP.NET StartKit TimeTracker中的方式和我以上说的差不多
你存放角色数据的cookie是自己另外单独创建的


现在假如我们系统中有3 种角色:Service,Work,Manage

要是我们想在WebForm1.aspx禁止Service,Manage这2类角色的登陆用户访问,我们可以在Web.config文件中做下面设置:

<location path="WebForm1.aspx">
        <system.web>
            <authorization>
             <deny roles="Service,Manage" />
  <deny users="?" />
            </authorization>
        </system.web>
</location>

还有一种方式就是:建立三个文件夹,某一角色的人只能访问某一文件夹里的ASPX.NET文件


假如我有用户a,他有Service,Work这2种角色,假如有页面abc.aspx,它允许Work,Manage这2种角色的用户访问。
个人感觉这样设置的灵活性不好,有没有通过代码控制的方法呢?
我编写了如下代码:实现单用户可以多角色,单页面多角色访问。

让我们先看Global.asax.cs中代码,请注意看事件Application_AuthenticateRequest中的代码实现。

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Threading;
None.gif
using  System.Globalization;
None.gif
using  System.Configuration;
None.gif
None.gif
namespace  BluepieCustomerService 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Global 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Global : System.Web.HttpApplication
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.IContainer components = null;
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 本系统自定义的角色之一“服务人员”
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string ConstUserRoleNameService="Service";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 本系统自定义的角色之一“普通工作人员”
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string ConstUserRoleNameWork="Work";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 本系统自定义的角色之一“管理人员”
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string ConstUserRoleNameManage="Manage";
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 逗号字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string ConstStringComma=",";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 百分号字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string ConstStringPercent="%";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// char 类型逗号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const char ConstCharComma=',';
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// char 类型百分号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const char ConstCharPercent='%';
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发生权限访问错误时,转向的错误提示页面
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string ConstRoleErrorPageName="RoleError.aspx?Index=-1";
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// DB的连接字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public const string  ConstWebConfigFileKeyName_ConnectionString="ConnectionString";
InBlock.gif                
InBlock.gif        
public Global()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }
    
InBlock.gif        
InBlock.gif        
protected void Application_Start(Object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                        
ExpandedSubBlockEnd.gif        }

InBlock.gif 
InBlock.gif        
protected void Session_Start(Object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Application_BeginRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Application_EndRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (HttpContext.Current.User!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
//用户已经通过验证
InBlock.gif
                if (Request.IsAuthenticated ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//得到用户的角色Cookie的名称
InBlock.gif
                    string userRolesCookieName=FormsAuthentication.FormsCookieName;
InBlock.gif                    
//得到用户的角色Cookie
InBlock.gif
                    string currentCookieValue=Context.Request.Cookies[userRolesCookieName].Value;
InBlock.gif                    
//解密
InBlock.gif
                    FormsAuthenticationTicket currentFormsAuthenticationTicket = FormsAuthentication.Decrypt(currentCookieValue);
InBlock.gif
InBlock.gif                    
//得到cookie中的用户数据
InBlock.gif
                    string[] userData = BCSTool.StringToArray(currentFormsAuthenticationTicket.UserData,ConstCharPercent);
InBlock.gif                    
InBlock.gif                    
//取得用户的个人详细信息数组                
InBlock.gif
                    int userId=Convert.ToInt32( userData[0]);
InBlock.gif                    
string userDisPlayName=userData[1];
InBlock.gif                    
string userName=userData[2];
InBlock.gif                    
string userEmail=userData[3];
InBlock.gif                    
InBlock.gif                    
//按当初加入的规则分解为数组
InBlock.gif
                    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值