> + System role and permission analysis ASP.NET implementation

xiaoxiao2021-03-06  18

.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.

''

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' ' User ID number

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created' ''

'' ----------------------------------------------- ------------------------------

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

''

'' ' Login account

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' '

'' '

''

'' '' '' [mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' ' Role Name

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' ' license ID number

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '' '' ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------

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

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property Roles () Property Roles () AS ArrayList

Get

Return me._rolelist

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' 'Returns a list of licensing

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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) '' '

'' ' Login account

'' ' login password

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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.

''

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------ 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

''

'' ' Login account

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '' '' ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------

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

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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,

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

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)

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property EmailAddress () Property EmailAddress () AS String

Get

Return me._emailaddress

END GET

End Property

'' ----------------------------------------------- ---------------------------- "''

'' 'Back to password

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property Password () Property Password () AS STRING

Get

Return me._password

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' 'Returning to the ID number of the user

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------

Public Readonly Property UserId () Property UserId () AS Integer

Get

Return Me._Userid

END GET

End Property

'' ----------------------------------------------- ------------------------------

'' '

'' 'User's theme information

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-14 created

'' '

'' ----------------------------------------------- ------------------------------ 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

''

'' '

''

'' '

'' '[Mrhjw] 2005-3-11 created

'' '

'' ----------------------------------------------- ------------------------------

Public Class AppPPAGECLASS AppPpege

Inherits System.Web.ui.page

'' ----------------------------------------------- ------------------------------

'' '

'' '1: Initialize custom verification; Second: Add an error commission

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-11 created

'' '

'' ----------------------------------------------- ------------------------------ 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

''

''

'' '

'' '

''

'' '

'' '[Mrhjw] 2005-3-11 created

'' '

'' ----------------------------------------------- ------------------------------

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

''

'' ' ' '

''

'' '

'' '[Mrhjw] 2005-3-11 created

'' '

'' ----------------------------------------------- ------------------------------

Protected Sub showerror () Sub showerror (ByVal Currenterror As Exception)

DIM Context as httpcontext = httpcontext.current

Context.response.write ("

& CurrentError.Message.trim & " ")

End Sub

#End region

END CLASS

End Namespace

Login call: login.aspx.vb

Private sub Link_log_click () Sub link_log_click (Byval e as system.EventArgs) Handles Link_log.click

Dim newuser as siteprincipal = siteprincipal.validatelogin (Textuser.Text.trim, txtpass.text.trim)

IF newuser is nothing then

Alert (TextUser.Text.trim "account login failed! Please try again!", Page

Else

CONTEXT.USER = NewUser

Formsauthentication.SetAuthCookie (Textuser.Text.trim, False)

Response.Redirect ("default.aspx")

END IF

End Sub

Permission management application:

Public Class AuditingOrderclass AuditingOrder

Inherits apppage

Protected Overrides Sub Oninit () Sub Oninit (Byval E As Eventargs)

MyBase.oninit (e)

End Sub

PRIVATE SUB Page_Load () Sub Page_Load (Byval E AS System.EventArgs) Handles MyBase.Load

IF not ispostback.

'// check permission

IF ctype (context.user, accounts.business.siteprincipal) .HASPERMISSION (CINT (Accounts.businessPermissions.ManagerOrder) = false tellonse.redirect ("../ WebPage / Limit.aspx")

Else

'// Bind the drop-down list box

BindactiveList ()

Over.text = dateadd (DateInterVal.day, 1, now ()). Tostring ("D")

Start.text = dateadd (DateInterVal.Mont, -1, now ()). Tostring ("D")

'// Load orders that need to be processed

BindMustOrderlist ()

END IF

END IF

End Sub

Finally attach a database chart:

User role table (Accounts_USERROLES)

Role table (Accounts_Roles)

Role license table (Accounts_Rolepermissions)

Specific license table (Accounts_Permissions)

The license class table (this table can be available, just for the provision of the Accounts_Permissions table]

Also defined enumeration objects

Public Enum AccountsPermissions

CreateNewUsers = 100 '// Create a new account

DeleteUsers = 101 '// Delete account

Managerroles = 102 '// Management role

ManagerUsers = 104 '// Manage users

ManagerDepartment = 105 '// management department

END ENUM

(Be sure to pay attention to the record of the database, that is, Accounts_Permissions Table)

Original address:

http://www.cnblogs.com/exhjw/articles/121231.html

转载请注明原文地址:https://www.9cbs.com/read-44103.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.050, SQL: 9