ASP.NET的HttpModule

开发 后端
本文介绍ASP.NET的HttpModule,HttpModules实现了类似于ISAPI Filter的功能,包括实现Init 方法,并且注册需要的方法等。

这段时间项目进入结束阶段,一直处于空闲状态,没事就把以前收集的一些代码研究了一下,发现ASP.NET真的是很强大,光一个web.config,我要真正透彻的了解它,还要花点功夫,刚刚稍微看了一下HttpModule, 做了个小程序,写下来,当作自己的学习笔记吧。
HttpModules实现了类似于ISAPI Filter的功能,在开发上,通常需要经过以下步骤:
1.编写一个类,实现IhttpModule接口
2.实现Init 方法,并且注册需要的方法
3.实现注册的方法
4.实现Dispose方法,如果需要手工为类做一些清除工作,可以添加Dispose方法的实现,但这不是必需的,通常可以不为Dispose方法添加任何代码。
5.在Web.config文件中,注册编写的类

关于Forms身份验证,网上的说明已经很多了,下面便开始做这个小小的角色控制程序。首先新建asp.net项目,并添加Login.aspx, Index1.aspx,index1.aspx, default.aspx等页面。添加一个存储用户信息的xml文件,在里面保存用户名,密码,用户角色等信息,类似这样:

  1. <UsersInfo> 
  2. <user name="admin" password="admin" role="admin" /> 
  3. <user name="user" password="user" role="user" /> 
  4. </UsersInfo> 

然后在web.config文件中的system.web节点中,按照下面的代码修改authentication节点,将身份验证方式设置为forms身份验证,并将登陆页面设置为

  1. <authentication mode="Forms"> 
  2. <forms name="TestAuth" loginUrl="Login.aspx" protection="None" timeout="60" /> 
  3. </authentication> 

另外再按如下方式增加如下节点控制用户对页面的访问的控制:

  1. <location path="Index1.aspx"> 
  2. <system.web> 
  3. <authorization> 
  4. <deny users="?" roles="user"/> 
  5. </authorization> 
  6. </system.web> 
  7. </location> 


接下来在login.aspx.cs中,加上对登陆button的click事件处理函数,这里我们在IsAuthenticated方法中检查用户名密码是否通过验证,并在通过验证后取得xml文件的roles信息,然后生成 FormsAuthenticationTicket,并将roles信息保存在ticket的userdata中,然后将ticket加入到客户端的 cookie中,同时重定向到用户最初请求的页面。

  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3. if(this.IsAuthenticated(TextBox1.Text,TextBox2.Text))  
  4. {  
  5. string userId = TextBox1.Text;  
  6. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
    (1,userId,DateTime.Now,DateTime.Now.AddSeconds(30),false,roles);  
  7. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
    FormsAuthentication.Encrypt(ticket));  
  8. Response.Cookies.Add(cookie);  
  9. Response.Redirect(FormsAuthentication.GetRedirectUrl(userId,false),true);  
  10. }  


然后在项目中添加新类,继承IHttpModule接口,实现Init 方法,并且注册需要的方法 :

  1. namespace WebApplication1  
  2. {  
  3. /**//// <summary> 
  4. /// Summary description for AuthenticationModule.  
  5. /// </summary> 
  6.     public class AuthenticationModule : IHttpModule  
  7. {  
  8. public AuthenticationModule()  
  9. {  
  10. //  
  11. // TODO: Add constructor logic here  
  12. //  
  13. }  
  14. private void Authentication_Request(object sender,EventArgs e)  
  15. {  
  16. HttpApplication App = (HttpApplication) sender;  
  17. HttpContext Ctx = App.Context ;  
  18. if (Ctx.Request.IsAuthenticated == true)  
  19. {  
  20. FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;  
  21. FormsAuthenticationTicket Ticket = Id.Ticket ;  
  22. string[] Roles = Ticket.UserData.Split (',') ;  
  23. Ctx.User = new GenericPrincipal (Id, Roles) ;  
  24. }  
  25. }  
  26. IHttpModule Members#region IHttpModule Members  
  27. void IHttpModule.Init(HttpApplication context)  
  28. {  
  29. context.AuthenticateRequest += new EventHandler(this.Authentication_Request);  
  30. }  
  31. void IHttpModule.Dispose()  
  32. {  
  33. }  
  34. #endregion  
  35. }  

在上面的Authentication_Request方法创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。

最后,请务必在web.config中注册你刚编写的AuthenticationModule类,位置在刚才修改身份验证方式的system.web的节点下,添加如下代码:

  1. <httpModules> 
  2. <add name="AuthenticationModule" type="WebApplication1.
    AuthenticationModule, WebApplication1"
     /> 
  3. </httpModules>  

大功告成,现在可以编译通过后,将index1.aspx设置为起始页,运行一下,是不是重定向到login.aspx页面了?然后分别用user和admin登陆,看看效果。以上介绍ASP.NET的HttpModule

【编辑推荐】

  1. ASP.NET的TypeConverter
  2. 浅析ASP.NET的TypeResolver
  3. ASP.NET中定义JavaScriptConverter
  4. 在ASP.NET中替换Sys.Services的方法
  5. 使用ASP.NET AJAX的Profile Service
责任编辑:佚名 来源: IT168
相关推荐

2009-07-22 17:45:35

ASP.NET教程

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入门教程

2009-07-29 11:19:03

JavaScriptASP.NET

2009-07-29 14:52:12

IScriptContASP.NET

2009-07-29 16:33:28

GreeterLogiASP.NET

2009-07-27 10:35:33

TypeConvertASP.NET

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-29 17:11:25

ASP.NET ISA

2009-07-29 16:08:07

ASP和ASP.NET

2009-07-22 18:03:00

ASP.NET ASP

2009-07-29 17:29:46

ASP与ASP.NET

2009-08-03 13:38:18

ASP.NET编程模型

2011-09-22 10:58:56

ASP.NET

2009-07-20 12:59:53

ASP.NET MVCASP.NET框架的功

2009-07-28 15:04:34

PHP ASP.NET

2009-07-22 17:23:03

XmlDataSourASP.NET 2.0

2009-07-29 10:19:48

Session StaASP.NET

2009-07-27 15:34:11

MembershipASP.NET
点赞
收藏

51CTO技术栈公众号