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

来源:互联网 发布:百度地图虎鲸数据平台 编辑:程序博客网 时间:2024/06/15 20:02
最近开始看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

现在假如我们系统中有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];
                    
                    
//按当初加入的规则分解为数组
                    string [] roleArray= BCSTool.StringToArray(userData[4],ConstCharComma );

                    
//设置当前 HTTP 安全信息
                    Context.User = new BCSLoginPrincipal(userId,
                                                        userDisPlayName,
                                                        userName,
                                                        userEmail,
                                                        roleArray,
                                                        HttpContext.Current.User.Identity);
                }

            }

        }


        
protected void Application_Error(Object sender, EventArgs e)
        
{

        }


        
protected void Session_End(Object sender, EventArgs e)
        
{

        }


        
protected void Application_End(Object sender, EventArgs e)
        
{

        }

            
        
Web 窗体设计器生成的代码
    }

}



让我们再看类BCSLoginPrincipal
实现了接口:IPrincipal,该接口定义用户对象的基本功能。
该接口有一个属性IIdentity Identity {get;},获取当前用户的标识。
一个方法bool IsInRole(string role),确定当前用户是否属于指定的角色。
using System;
using System.Security.Principal;

namespace BluepieCustomerService
{
    
/// <summary>
    
/// BCSLoginPrincipal 的摘要说明。
    
/// </summary>

    public class BCSLoginPrincipal :System.Security.Principal.IPrincipal 
    
{
        
private string[] _userRole;
        
        
protected IIdentity _iIdentity;

        
int _userId;
        
string    _userDisPlayName;
        
string  _userName;
        
string  _userEmail;
        
        
/// <summary>
        
/// 类BCSLoginPrincipal的有参构造器
        
/// </summary>
        
/// <param name="iIdentity"></param>
        
/// <param name="userRole"></param>

        public BCSLoginPrincipal(int userId,string userDisPlayName,string userName,string userEmail    , string[] userRole,IIdentity iIdentity)
        
{
            
this._userId=userId;
            
this._userDisPlayName=userDisPlayName;
            
this._userName=userName;
            
this._userEmail=userEmail;
            
this._userRole=userRole;
            
this._iIdentity=iIdentity;
        }


        
/// <summary>
        
/// 取得和设置登陆用户的UserID
        
/// </summary>

        public int UserId
        
{
            
get
            
{
                
return _userId;
            }

            
set
            
{
                _userId
=value;
            }

        }


        
/// <summary>
        
/// 取得和设置登陆用户的登陆帐号
        
/// </summary>

        public string UserDisPlayName
        
{
            
get
            
{
                
return _userDisPlayName;
            }

            
set
            
{
                _userDisPlayName
=value;
            }

        }


        
/// <summary>
        
/// 取得和设置登陆用户的真实姓名
        
/// </summary>

        public string UserName
        
{
            
get
            
{
                
return _userName;
            }

            
set
            
{
                _userName
=value;
            }

        }


        
/// <summary>
        
/// 取得和设置登陆用户的Email
        
/// </summary>

        public string UserEmail
        
{
            
get
            
{
                
return _userEmail;
            }

            
set
            
{
                _userEmail
=value;
            }

        }


        
        
/// <summary>
        
/// 取得和设置登陆用户的角色数组
        
/// </summary>

        public string[] UserRole
        
{
            
get 
            
{
                
return _userRole;
            }

            
set 
            
{
                _userRole 
= value; 
            }

        }


        
public IIdentity Identity
        
{
            
get    
            
{
                
return _iIdentity;
            }

            
set    
            
{
                _iIdentity 
= value; 
            }

        }


        
/// <summary>
        
/// 实现接口方法,判断用户的角色是否合法
        
/// </summary>
        
/// <param name="role"></param>
        
/// <returns></returns>

        public bool IsInRole(string role)
        
{
            
//用户传过来的角色字符串有可能包含多个角色,所以我们得先分解为数组
            string [] roleArray = BCSTool.StringToArray(role,Global.ConstCharComma);
            
//取得数组长度
            int roleArrayLength=roleArray.Length;

            
//取出数组中每一个角色与用户的现有角色做比较
            for(int i=0;i<roleArrayLength;i++)
            
{
                
if( isRole( roleArray[i] ) )
                
{
                    
return true;
                }

            }

            
return false;
        }


        
/// <summary>
        
/// 与用户的现有角色做比较
        
/// </summary>
        
/// <param name="str"></param>
        
/// <returns></returns>

        bool isRole(string str)
        
{
            
int arrayLength=_userRole.Length;

            
for(int i=0;i<arrayLength;i++)
            
{
                
if ( _userRole[i]==str )
                
{
                    
return true;
                }

            }

            
return false;
        }


    }

}


原创粉丝点击