.NET has provided us with the operation of system role and permissions, but the implementation of the scheme is not very detailed, so we can make our needs. The information saved in context.user is the relevant role and permission information. Context.user type is system.security.principal.iprincipal; context.user.Identity is System.Security.Principal.iIdentity, so as long as the above two interfaces we implement can realize our required solutions
First define class siteprincipal code as follows:
Imports system
Imports system.Web
'' ----------------------------------------------- ------------------------------
'' 'Project: Accounts.business
'' Class: WebModules.accounts.business.siteprincipal
'' '
'' ----------------------------------------------- ------------------------------
'' '
'' 'This class implements this site authority management by implementing the [System.Security.Principal.iIndentity] interface.
'' Summary>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Class Siteprincipalclass SiteprIncipal
Implements system.security.principal.Iprincipal
#Region "Private Variable"
Private_Identity As System.Security.principal.iIndentity
Private_PermissionList as arraylist
Private _rolelist as arraylist
#End region
#Region "Constructor"
'' ----------------------------------------------- ------------------------------
'' '
'' Is instantiated by the user ID number
'' Summary>
'' ' User ID number param>
'' '
'' Remarks>
'' '
'' '[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
'' ----------------------------------------------- ------------------------------
'' '
'' 'Is instantiated by logging in account
'' Summary>
'' ' Login account param>
'' '
'' Remarks>
'' '
'' '[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, siteident) .userid
Me._PermissionList = User.GeteffECTIVEPERMISSIONLIST (ctype (me._identity, siteident) .userid
End Sub
#End region
#REGON "[system.semburity.principal.iprincipal] implementation method"
'' ----------------------------------------------- ------------------------------
'' '
Identity implementation method of '' '[system.security.principal.iprincipal] interface
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property Identity () Property Identity () as system.security.principal.iidentity.principal.iprincipal.Identity.Principal.IPrincipal.Identity
Get
Return Me._Identity
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
'' [System.security.principal.iprincipal] interface isinrole implementation method
'' Summary>
'' ' Role Name param>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public function isinrole () Function isinrole (byval role as string) as boolean imports system.security.principal.iprincipal.Isinrole
Return Me._RoleList.Contains (Role)
END FUNCTION
#End region
#Region "New Attribute"
'' ----------------------------------------------- ------------------------------
'' '
'' Verify that there is a corresponding license
'' Summary>
'' ' license ID number param>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>' '' ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------
Public Readonly Property Haspermission () Property Haspermission (Byval Permissionid As INTEGER) AS BOOLEAN
Get
Return Me._PermissionList.Contains (PermissionID)
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
'' 'Returns a list of roles
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property Roles () Property Roles () AS ArrayList
Get
Return me._rolelist
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
'' 'Returns a list of licensing
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property Permissions () Property Permissions () ARRAYLIST
Get
Return me._permissionlist
END GET
End Property
#End region
#Region "New Method"
'' ----------------------------------------------- ------------------------------
'' '
'' Verification Login. Iif (Sign in Success, Return SitePrincipal, Return Nothing) '' ' summary>
'' ' Login account param>
'' ' login password param>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Shared Function Validatelogin () Function Validatelogin (Byval Password As String) AS Business.SiteprIncipal
DIM User as data.user
DIM NewID AS INTEGER
Newid = User.validatalogin (EmailAddress, Password)
IF newid> 0 THEN
Return New Siteprincipal (EmailAddress)
Else
Return Nothing
END IF
END FUNCTION
#End region
END CLASS
Then define class siteidentity, the code is as follows:
Imports system
Imports system.Web
Imports mrhjw.components
'' ----------------------------------------------- ------------------------------
'' 'Project: Accounts.business
'' Class: WebModules.accounts.business.siteIdentity
'' '
'' ----------------------------------------------- ------------------------------
'' '
'' 'This class implements this site authority management by implementing the [System.Security.Principal.iIndentity] interface.
'' Summary>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------ Public Class SiteIdentityClass SiteIdentity
Implements system.security.principal.iidentity.principal.iIdentity.principal.iz
#Region "Private Variable"
Private _username as string '// username
Private_emailaddress as string '// account
Private _password as string '// password
Private _Userid as integer '// user ID number
Private _theme as string = string.empty '// User's theme _ default is' default'
#End region
#Region "Constructor"
'' ----------------------------------------------- ------------------------------
'' '
'' 'Is instantiated by logging in account
'' Summary>
'' ' Login account param>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Sub New () Sub New (ByVal Emailaddress As String)
DIM User as new data.user
DIM DR AS DATAROW
DR = User.Retrieve (EmailAddress)
'' to do something
'' '
'' '
'' '
'' '
AppGlobals.skin = me._theme
End Sub
'' ----------------------------------------------- ------------------------------
'' '
'' Is instantiated according to user ID
'' Summary>
'' ' param>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>' '' ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------
Public Sub New () Sub New (Byval Userid AS Integer)
DIM User as new data.user
DIM DR AS DATAROW
DR = User.Retrieve (userid)
'' to do something
'' '
'' '
'' '
'' '
'' '
AppGlobals.skin = me._theme
End Sub
#End region
#Region "[system.semburity.principal.iidentity] implementation method" "
'' ----------------------------------------------- ------------------------------
'' '
AuthenticationType implementation of '' '' system.security.principal.iidentity interface
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property AuthenticationType () Property AuthenticationType () AS String Implements System.security.principal.iIdentity.AuthenticationType
Get
Return "Custom AuthenticationType"
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
The isauthenticated implementation of '' 'system.security.principal.iidentity interface, returns true, because this class must be used after verification,
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property Isauthenticated () Property IsAuthenticated () as boolean imports system.security.principal.iidentity.isauthenticated
Get
Return True
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
'' 'System.security.principal.iidentity interface Name implementation
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property Name () Property Name () AS String Implements System.security.principal.iIdentity.name
Get
Return me._username
END GET
End Property
#End region
#Region "New Attribute"
'' ----------------------------------------------- ------------------------------
'' '
'' 'Back to login account (EmailAddress)
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property EmailAddress () Property EmailAddress () AS String
Get
Return me._emailaddress
END GET
End Property
'' ----------------------------------------------- ---------------------------- "''
'' 'Back to password
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property Password () Property Password () AS STRING
Get
Return me._password
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
'' 'Returning to the ID number of the user
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Readonly Property UserId () Property UserId () AS Integer
Get
Return Me._Userid
END GET
End Property
'' ----------------------------------------------- ------------------------------
'' '
'' 'User's theme information
'' Summary>
'' '
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-14 created
'' ' History>
'' ----------------------------------------------- ------------------------------ Public Readonly Property Theme () Property Theme () AS STRING
Get
Return me._theme
END GET
End Property
#End region
'// the end
END CLASS
Then we define a page base class so that all the pages inherit it.
Appppage.vb code is as follows:
Imports system
Imports system.Web
Namespace ComponentsNamespace Components.Web
'' ----------------------------------------------- ------------------------------
'' 'PROJECT: SimpleDemo
'' Class: Components.Web.Appage
'' '
'' ----------------------------------------------- ------------------------------
'' '
'' 'This class is the base class for all page files of this site,
'' 'All pages are directly or indirectly inherited.
'' 'This category is implemented to the extension of Page by inheriting system.web.ui.page
'' 'For adding page features, just modify this class
'' Summary>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-11 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Public Class AppPPAGECLASS AppPpege
Inherits System.Web.ui.page
'' ----------------------------------------------- ------------------------------
'' '
'' '1: Initialize custom verification; Second: Add an error commission
'' Summary>
'' ' param>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-11 created
'' ' History>
'' ----------------------------------------------- ------------------------------ Protected Overrides Sub Oninit () Sub Oninit (Byval E ASTEM.EventArgs)
If Context.user.Identity.isauthenticated = TRUE THEN
If Not (TypeOf Context.user IS Webmodules.accounts.business.sitePrincipal) THEN
Dim newuser as new webmodules.accounts.business.siteprincipal (context.user.Identity.name)
CONTEXT.USER = NewUser
END IF
END IF
'// Add an error commission
'AddHandler page. error, addressof me.philepage_error
End Sub
#Region "Abnormal Handling"
'' ----------------------------------------------- ------------------------------
'' '
'' 'Abnormal Event Processing
'' Summary>
'' param>
'' ' param>
'' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-11 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Protected Sub PhilePage_ERROR () Sub PhilePage_ERROR (Byval E AS Object, Byval E As Eventargs)
DIM CurrentError As Exception = Page.Server.getlasterror ()
IF not (TypeOf Currenterror Is Appexception.Appexception) THEN
MRHJW.APPEXCEPTION.APPEXCEPTION.LOGERROR (CurrentError.Message.toString)
END IF
Showerror (Currenterror)
Page.server.clearerror ()
End Sub
'' ----------------------------------------------- ------------------------------
'' '
'' Custom Display Friendly Error Message
'' Summary>
'' ' param>' '
'' Remarks>
'' '
'' '[Mrhjw] 2005-3-11 created
'' ' History>
'' ----------------------------------------------- ------------------------------
Protected Sub showerror () Sub showerror (ByVal Currenterror As Exception)
DIM Context as httpcontext = httpcontext.current
Context.response.write ("