`

代码阅读总结之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中的代码实现。

using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Threading;
using System.Globalization;
using System.Configuration;

namespace BluepieCustomerService 
{
    
/**//// <summary>
    
/// Global 的摘要说明。
    
/// </summary>
    public class Global : System.Web.HttpApplication
    
{
        
/**//// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.IContainer components = null;
        
        
/**//// <summary>
        
/// 本系统自定义的角色之一“服务人员”
        
/// </summary>
        public const string ConstUserRoleNameService="Service";

        
/**//// <summary>
        
/// 本系统自定义的角色之一“普通工作人员”
        
/// </summary>
        public const string ConstUserRoleNameWork="Work";

        
/**//// <summary>
        
/// 本系统自定义的角色之一“管理人员”
        
/// </summary>
        public const string ConstUserRoleNameManage="Manage";
        
        
/**//// <summary>
        
/// 逗号字符串
        
/// </summary>
        public const string ConstStringComma=",";

        
/**//// <summary>
        
/// 百分号字符串
        
/// </summary>
        public const string ConstStringPercent="%";

        
/**//// <summary>
        
/// char 类型逗号
        
/// </summary>
        public const char ConstCharComma=',';

        
/**//// <summary>
        
/// char 类型百分号
        
/// </summary>
        public const char ConstCharPercent='%';

        
/**//// <summary>
        
/// 发生权限访问错误时,转向的错误提示页面
        
/// </summary>
        public const string ConstRoleErrorPageName="RoleError.aspx?Index=-1";
        
        
/**//// <summary>
        
/// DB的连接字符串
        
/// </summary>
        public const string  ConstWebConfigFileKeyName_ConnectionString="ConnectionString";
                
        
public Global()
        
{
            InitializeComponent();
        }
    
        
        
protected void Application_Start(Object sender, EventArgs e)
        
{
                        
        }

 
        
protected void Session_Start(Object sender, EventArgs e)
        
{
            
        }


        
protected void Application_BeginRequest(Object sender, EventArgs e)
        
{

        }


        
protected void Application_EndRequest(Object sender, EventArgs e)
        
{

        }


        
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        
{
            
if (HttpContext.Current.User!=null)
            
{    
                
//用户已经通过验证
                if (Request.IsAuthenticated ) 
                
{
                    
//得到用户的角色Cookie的名称
                    string userRolesCookieName=FormsAuthentication.FormsCookieName;
                    
//得到用户的角色Cookie
                    string currentCookieValue=Context.Request.Cookies[userRolesCookieName].Value;
                    
//解密
                    FormsAuthenticationTicket currentFormsAuthenticationTicket = FormsAuthentication.Decrypt(currentCookieValue);

                    
//得到cookie中的用户数据
                    string[] userData = BCSTool.StringToArray(currentFormsAuthenticationTicket.UserData,ConstCharPercent);
                    
                    
//取得用户的个人详细信息数组                
                    int userId=Convert.ToInt32( userData[0]);
                    
string userDisPlayName=userData[1];
                    
string userName=userData[2];
                    
string userEmail=userData[3];
                    
                    
//按当初加入的规则分解为数组
                    
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics