现在的位置: 首页 > 综合 > 正文

基于角色的用户验证[翻译]

2013年10月08日 ⁄ 综合 ⁄ 共 2006字 ⁄ 字号 评论关闭

Introduction

 


 

这篇文章示范了如何使用asp.net中的表单验证。我写了一些类和一个使用这些类的小型web应用程序作示例。这个程序重点写了四个pages以实现这些功能:增加用户,给用户指派角色,给用户移除角色及管理角色。虽然我写的这些类提供了足够使用的功能,但为演示,我限制了User类的字段。这意味着,用户新注册账户时将提供基本的字段如:全名,Email,密码,个人简介等。如果以后需要,你可以很容易地增加其它字段。

 

The Classes Overview

 

这里有User, Role, SitePrincipal SiteIdentity 四个类,下面是这些类的属性和方法:

 

 

The User class

 


User()

 

 

Default parameter less constructor to create a new user

 

 

User(int userID)

 

 

This constructor gets a userID and looks up the user details from the database

 

 

User(string email)

 

 

This constructor gets an email and looks up the user details from the database

 

 

GetUsers()

 

 

This method returns a DataSet of all the users available in the database

 

 

GetRoles()

 

 

This method returns a DataSet of roles assigned to the current user

 

 

GetUserRoles(int userID)

 

 

This static method grabs the userID and returns a roles ArrayList assigned to that user

 

 

AddToRole(int roleID)

 

 

This method assigns a role to the current user

 

 

RemoveFromRole(int roleID)

 

 

This method removes current user from the role that has been passed by the roleID.

 

 

Add()

 

 

Adds a new user to the database

 

 

Update()

 

 

Updates current user information

 

 

Delete()

 

 

Deletes current user

 

 

UserID

 

 

Gets/Sets user's id number

 

 

FullName

 

 

Gets/Sets user's full name

 

 

Email

 

 

Gets/Sets user's email

 

 

Password

 

 

Gets/Sets user's password

 

 

Biography

 

 

Gets/Sets user's biography

 

 

DateAdded

 

 

Gets/Sets user's registering date

 

 

The Role class

 

Role()

 

 

Default parameter less constructor to create a new role

 

 

Role(int roleID)

 

 

This constructor gets a roleID and looks up the role details from the database

 

 

GetRoles()

 

 

This method returns a DataSet of all roles available in the database

 

 

Add()

 

 

Adds a new role to the database

 

 

Update()

 

 

Updates current role information

 

 

Delete()

 

 

Deletes current role

 

 

RoleID

 

 

Gets/Sets role ID number

 

 

RoleName

 

 

Gets/Sets role name

 

 

The SitePrincipal class (implements the IIPrincipal Interface)

 

SitePrincipal(int userID)

 

 

This constructor gets a userID and looks up details from the database

 

 

SitePrincipal(string email)

 

 

This constructor gets an email and looks up details from the database

 

 

IsInRole()

 

 

(IIPrincipal.IsInRole()) Indicates whether a current principal is in a specific role

 

 

ValidateLogin()

 

 

Adds a new user to the database

 

 

抱歉!评论已关闭.