自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET的HttpModule

開(kāi)發(fā) 后端
本文介紹ASP.NET的HttpModule,HttpModules實(shí)現(xiàn)了類(lèi)似于ISAPI Filter的功能,包括實(shí)現(xiàn)Init 方法,并且注冊(cè)需要的方法等。

這段時(shí)間項(xiàng)目進(jìn)入結(jié)束階段,一直處于空閑狀態(tài),沒(méi)事就把以前收集的一些代碼研究了一下,發(fā)現(xiàn)ASP.NET真的是很強(qiáng)大,光一個(gè)web.config,我要真正透徹的了解它,還要花點(diǎn)功夫,剛剛稍微看了一下HttpModule, 做了個(gè)小程序,寫(xiě)下來(lái),當(dāng)作自己的學(xué)習(xí)筆記吧。
HttpModules實(shí)現(xiàn)了類(lèi)似于ISAPI Filter的功能,在開(kāi)發(fā)上,通常需要經(jīng)過(guò)以下步驟:
1.編寫(xiě)一個(gè)類(lèi),實(shí)現(xiàn)IhttpModule接口
2.實(shí)現(xiàn)Init 方法,并且注冊(cè)需要的方法
3.實(shí)現(xiàn)注冊(cè)的方法
4.實(shí)現(xiàn)Dispose方法,如果需要手工為類(lèi)做一些清除工作,可以添加Dispose方法的實(shí)現(xiàn),但這不是必需的,通??梢圆粸镈ispose方法添加任何代碼。
5.在Web.config文件中,注冊(cè)編寫(xiě)的類(lèi)

關(guān)于Forms身份驗(yàn)證,網(wǎng)上的說(shuō)明已經(jīng)很多了,下面便開(kāi)始做這個(gè)小小的角色控制程序。首先新建asp.net項(xiàng)目,并添加Login.aspx, Index1.aspx,index1.aspx, default.aspx等頁(yè)面。添加一個(gè)存儲(chǔ)用戶信息的xml文件,在里面保存用戶名,密碼,用戶角色等信息,類(lèi)似這樣:

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

然后在web.config文件中的system.web節(jié)點(diǎn)中,按照下面的代碼修改authentication節(jié)點(diǎn),將身份驗(yàn)證方式設(shè)置為forms身份驗(yàn)證,并將登陸頁(yè)面設(shè)置為

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

另外再按如下方式增加如下節(jié)點(diǎn)控制用戶對(duì)頁(yè)面的訪問(wèn)的控制:

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


接下來(lái)在login.aspx.cs中,加上對(duì)登陸button的click事件處理函數(shù),這里我們?cè)贗sAuthenticated方法中檢查用戶名密碼是否通過(guò)驗(yàn)證,并在通過(guò)驗(yàn)證后取得xml文件的roles信息,然后生成 FormsAuthenticationTicket,并將roles信息保存在ticket的userdata中,然后將ticket加入到客戶端的 cookie中,同時(shí)重定向到用戶最初請(qǐng)求的頁(yè)面。

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


然后在項(xiàng)目中添加新類(lèi),繼承IHttpModule接口,實(shí)現(xiàn)Init 方法,并且注冊(cè)需要的方法 :

  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方法創(chuàng)建一個(gè) FormsIdentity 對(duì)象和一個(gè) GenericPrincipal 對(duì)象。前一個(gè)對(duì)象從票名稱(chēng)獲得用戶名,后一個(gè)對(duì)象將此標(biāo)識(shí)與用戶角色列表包含在一起。

最后,請(qǐng)務(wù)必在web.config中注冊(cè)你剛編寫(xiě)的AuthenticationModule類(lèi),位置在剛才修改身份驗(yàn)證方式的system.web的節(jié)點(diǎn)下,添加如下代碼:

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

大功告成,現(xiàn)在可以編譯通過(guò)后,將index1.aspx設(shè)置為起始頁(yè),運(yùn)行一下,是不是重定向到login.aspx頁(yè)面了?然后分別用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
責(zé)任編輯:佚名 來(lái)源: IT168
相關(guān)推薦

2009-08-03 14:22:33

什么是ASP.NET

2009-07-28 17:17:19

ASP.NET概述

2009-07-22 17:45:35

ASP.NET教程

2009-07-27 10:35:33

TypeConvertASP.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 12:22:03

ASP.NET和ASPASP.NET入門(mén)教程

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-07-29 17:11:25

ASP.NET ISA

2009-07-29 16:08:07

ASP和ASP.NET

2011-09-22 10:58:56

ASP.NET

2009-08-03 17:35:07

ASP.NET WebASP.NET編程工具

2009-07-28 09:02:32

asp.net aja

2009-07-20 12:59:53

ASP.NET MVCASP.NET框架的功

2009-07-22 10:24:21

AsyncState參ASP.NET

2009-07-24 10:10:22

ASP.NET

2009-07-24 16:17:42

WebRequestEASP.NET

2009-07-28 13:17:09

EnableViewSASP.NET

2009-07-27 10:18:12

TypeResolveASP.NET
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)