转贴---》代码阅读总结之ASP.NET StartKit TimeTracker(角色权限)
来源:互联网 发布:百度地图虎鲸数据平台 编辑:程序博客网 时间:2024/06/15 20:02
最近开始看ASP.NET StartKit TimeTracker中代码,它是一个典型的项目追踪系统。
它比我前几天看的ASP.NET StartKit Commerce复杂了许多。
例如:在ASP.NET StartKit TimeTracker开始有明显的三层结构的设计。PL层,BLL层和DAL层。
同时开始在项目中引进了角色权限管理功能等等。
让我们再看类BCSLoginPrincipal
实现了接口:IPrincipal,该接口定义用户对象的基本功能。
该接口有一个属性IIdentity Identity {get;},获取当前用户的标识。
一个方法bool IsInRole(string role),确定当前用户是否属于指定的角色。
它比我前几天看的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 窗体设计器生成的代码#region Web 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
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 窗体设计器生成的代码#region Web 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
让我们再看类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;
}
}
}
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;
}
}
}
- 转贴---》代码阅读总结之ASP.NET StartKit TimeTracker(角色权限)
- 转贴---》代码阅读总结之ASP.NET StartKit TimeTracker(角色权限
- ASP.NET MVC4 自定义权限(角色)验证
- 解读ASP.NET TimeTracker Starter Kit(1)——数据库篇
- 解读ASP.NET TimeTracker Starter Kit(2)——重构篇
- 解读ASP.NET TimeTracker Starter Kit(1)——数据库篇
- 解读ASP.NET TimeTracker Starter Kit(2)——重构篇
- Asp.net中的用户角色权限设计
- ASP.net Membership角色与权限管理
- ASP.net Membership角色与权限管理
- ASP.NET Form验证和角色权限
- ASP.NET网站权限设计实现(二)——角色权限绑定
- ASP.NET MVC4 SimpleMembership 基于角色的权限管理机制(一)--基本配置与使用
- >+ 系统角色与权限分析 ASP.NET实现
- 系统角色与权限分析 ASP.NET实现
- ASP.NET MVC:窗体身份验证及角色权限管理示例
- ASP.net Membership角色与权限管理(转)
- ASP.NET MVC:窗体身份验证及角色权限管理示例
- 选数程序
- Java 编程技术中汉字问题的分析及解决(从根源上解决)
- 角色权限收藏 (转)
- 我购买的计算机书籍清单
- 编写一个对文件按字节写入的程序floppyWrite.exe
- 转贴---》代码阅读总结之ASP.NET StartKit TimeTracker(角色权限)
- 女孩,当你说没有好男人时请进来看看
- 用Java开源项目JOONE实现人工智能编程
- evolution
- 是时候好好做做记录
- VC学习之路
- 中专学历的我和女朋友在深圳2年的奋斗史(ZT)
- 年度十大卫星图像排行榜
- 《心经》