我翻译的:Roles-Based Authentication By Zek3vil
介绍
这篇文章演示了如何在ASP.NET中使用窗体验证。 我写了一些类和一个小的WEB应用程序作为例子。这个小程序有四个窗体(页)用来完成以下功能: 添加新用户, 给用户赋予角色, 从普通用户和管理员角色中移除角色。尽管我写的这些类提供了足够多的已经可以使用的功能,出于示范的目的,我限制了用户类的域。这意味着用户可以提供一些基本的域当注册一个新的帐号,例如:姓名,邮件,密码,个人介绍。如果你想的话,你也可以添加一些域这非常简单。
这四个类分别是: User, Role, SitePrincipal 和SiteIdentity.下面是这四个类的方法和属性:
The User class
|
|
Default parameter less constructor to create a new user |
|
|
This constructor gets a |
|
|
This constructor gets an email and looks up the user details from the database |
|
|
This method returns a |
|
|
This method returns a |
|
|
This static method grabs the |
|
|
This method assigns a role to the current user |
|
|
This method removes current user from the role that has been passed by the |
|
|
Adds a new user to the database |
|
|
Updates current user information |
|
|
Deletes current user |
|
|
Gets/Sets user's id number |
|
|
Gets/Sets user's full name |
|
|
Gets/Sets user's email |
|
|
Gets/Sets user's password |
|
|
Gets/Sets user's biography |
|
|
Gets/Sets user's registering date |
The Role class
|
|
Default parameter less constructor to create a new role |
|
|
This constructor gets a |
|
|
This method returns a |
|
|
Adds a new role to the database |
|
|
Updates current role information |
|
|
Deletes current role |
|
|
Gets/Sets role ID number |
|
|
Gets/Sets role name |
The SitePrincipal class (implements the IIPrincipal Interface)
|
|
This constructor gets a |
|
|
This constructor gets an email and looks up details from the database |
|
|
( |
|
|
Adds a new user to the database |
|
|
( |
|
|
Gets the roles of the current principal |
The SiteIdentity class (implements the IIdentity Interface)
|
|
This constructor gets a |
|
|
This constructor gets an email and looks up the user details from the database |
|
|
( |
|
|
( |
|
|
( |
|
|
Gets the email of the current user |
|
|
Gets the password of the current user |
|
|
Gets the user ID number of the current user |
使用窗体验证
要使用 ASP.NET Forms Authentication, 你的应用程序的 web.config 文件必须包含以下内容:
<configuration> <system.web> <authentication mode="Forms"> <forms name="RolesBasedAthentication" path="/" loginUrl="/Login.aspx" protection="All" timeout="30"> </forms> </authentication> </system.web></configuration> 把 authentication mode 设为 When Forms Authentication is enabled, 每次用户请求一个页面, the form will attempt to look up for a cookie in the user's browser. 如果找到了, the user identity was kept in the cookie represented in the
因为 创建 Login 页面 为了创建login 页面, 你仅仅需要两个 textboxes ,用来让用户输入邮件地址 和密码。 将它们分别命名为 private void Submit_Click(object sender, System.EventArgs e) {// 调用 ValidateLogin 静态方法来检查电子邮件地址和密码是否 // 正确。如果正确,这个方法将返回一个新的用户,否则返回null SitePrincipal newUser = SitePrincipal.ValidateLogin(Email.Text, Password.Text); if (newUser == null) { ErrorMessage.Text = "Login failed for " + Email.Text;ErrorMessage.Visible = true; } else {// 将新用户分配给当前的用户上下文 Context.User = newUser;// 将邮件地址放到 cookie 中 // true 代表 cookie 被设为永久保存 FormsAuthentication.SetAuthCookie( Email.Text, true ); // 将用户重新定位到主页 Response.Redirect("Default.aspx"); }} 以上的代码是简单易懂的。首先我们调用 对每一个请求验证用户
无论何时用户请求一个页面, ASP.NET Forms Authentication 将自动获取 cookie. 但是我们But we haven't replaced the current context user with our own, 因此我们要创建一个 public class PageBase: System.Web.UI.Page { public PageBase() { } protected override void OnInit(EventArgs e) { base.OnInit(e); this.Load += new System.EventHandler(this.PageBase_Load); } private void PageBase_Load(object sender, System.EventArgs e) { if (Context.User.Identity.IsAuthenticated) { if (!(Context.User is SitePrincipal)) { SitePrincipal newUser = new SitePrincipal( Context.User.Identity.Name ); Context.User = newUser; } } }} 因此现在每一页都源自这一基类而不是源自 if (Context.User.Identity.IsAuthenticated) { string name = ((SiteIdentity)Context.User.Identity).FullName; string email = ((SiteIdentity)Context.User.Identity).Email; string password = ((SiteIdentity)Context.User.Identity).Password; string userID = ((SiteIdentity)Context.User.Identity).UserID;} 或者,你可以使用以下方法来判断当前用户是否属于一个特定的角色: if (Context.User.Identity.IsAuthenticated) {// 如果用户不属于 Admin 角色, // 他/她将被重新定位到login 页面 if (!((SitePrincipal)Context.User).IsInRole("Site Admin")) Response.Redirect("Login.aspx");} The Demo Application
All the code above is the only base for using my classes to turn your application into a roles-based authentication system. How ever I have written a small demo web application that uses these classes as an example with quite enough functions like: insert/update/delete roles, assign user to roles and remove user from roles. In order to get the application up and running, you need to have SQL Sever, since I'm not using Access as a database management system. You can download the demo application and all the source code for the classes from the links at the top of this page and follow these steps to get the application up and running:
When running the application, log on with account: admin@site.com and password: admin to have full access. Hope you find this small application helpful. |
浙公网安备 33010602011771号