.net 中已经为我们提供了系统角色与权限的操作,只不过实现的方案不是很详尽,所以我们对它稍加修改则可以实现我们所需要的方案。context.User中保存的信息就是相关的角色与权限信息。Context.User类型为System.Security.Principal.IPrincipal;Context.User.Identity为System.Security.Principal.IIdentity,因此只要我们实现的上述的两个接口便可实现我们所需的方案
首先定义类SitePrincipal代码如下:
Imports System
Imports System.Web
''' ------------------------------------------------------
''' Project : Accounts.Business
''' Class : WebModules.Accounts.Business.SitePrincipal
'''
''' -----------------------------------------------------
''' <summary>
''' 本类通过实现[System.Security.Principal.IIdentity]接口来实现本站点权限管理
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [Mrhjw] 2005-3-14 Created
''' </history>
''' -------------------------------------------------------
Public Class SitePrincipalClass SitePrincipal
Implements System.Security.Principal.IPrincipal
#Region "私有变量"
Private _Identity As System.Security.Principal.IIdentity
Private _PermissionList As ArrayList
Private _RoleList As ArrayList
#End Region
#Region "构造函数"
''' -----------------------------------------------------
''' <summary>
''' 通过用户ID号来实例化
''' </summary>
''' <param name="UserID">用户ID号</param>
''' <remarks>
''' </remarks>
''' <history>
''' [Mrhjw] 2005-3-14 Created
''' </history>
''' ------------------------------------------------------
Public Sub New()Sub New(ByVal UserID As Integer)
Dim User As New Data.User
Me._Identity = New Business.SiteIdentity(UserID)
Me._RoleList = User.GetUserRoles(UserID)
Me._PermissionList = User.GetEffectivePermissionList(UserID)
End Sub
''' ------------------------------------------------------
''' <summary>
''' 通过登录帐号来实例化
''' </summary>
''' <param name="EmailAddress">登录帐号</param>
''' <remarks>
''' </remarks>
''' <history>
''' [Mrhjw] 2005-3-14 Created
''' </history>
''' ------------------------------------------------------
Public Sub New()Sub New(ByVal EmailAddress As String)
Dim User As New Data.User
Me._Identity = New Business.SiteIdentity(EmailAddress)
Me._RoleList = User.GetUserRoles(CType(Me._Identity, SiteIdentity).UserID)
Me._PermissionList = User.GetEffectivePermissionList(CType(Me._Identity, SiteIdentity).UserID)
End Sub
#End Region
#Region "[System.Security.Principal.IPrincipal]接口的实现方法"
''' -------------------------------------------------------
''' <summary>
''' [System.Security.Principal.IPrincipal]接口的Identity实现方法
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [Mrhjw] 2005-3-14 Created
''' </history>
''' -----------------------------------------------------
Public ReadOnly Property Identity()Property Identity() As System.Security.Principal.IIdentity Implements System.Security.Principal.IPrincipal.Identity
Get
Return Me._Identity
End Get
End Property
''' ----------------------------------------------------------
''' <summary>
''' [System.Security.Principal.IPrincipal]接口的IsInRole实现方法
''' </summary>
''' <param name="role">角色名称</param>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [Mrhjw] 2005-3-14 Created
''' </history>
''' ------------------------------------------------------
Public Function IsInRole()Function IsInRole(ByVal Role As String) As Boolean Implements System.Security.Principal.IPrincipal.IsInRole
Return Me._RoleList.Contains(Role)
End Function
#End Region
#Region "新增属性"
''' -------------------------------------------------------
''' <summary>
''' 验证是否有相应的许可
''' </summary>
'''