ASP.NET的HttpModule
這段時(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)似這樣:
- <UsersInfo>
- <user name="admin" password="admin" role="admin" />
- <user name="user" password="user" role="user" />
- </UsersInfo>
然后在web.config文件中的system.web節(jié)點(diǎn)中,按照下面的代碼修改authentication節(jié)點(diǎn),將身份驗(yàn)證方式設(shè)置為forms身份驗(yàn)證,并將登陸頁(yè)面設(shè)置為
- <authentication mode="Forms">
- <forms name="TestAuth" loginUrl="Login.aspx" protection="None" timeout="60" />
- </authentication>
另外再按如下方式增加如下節(jié)點(diǎn)控制用戶對(duì)頁(yè)面的訪問(wèn)的控制:
- <location path="Index1.aspx">
- <system.web>
- <authorization>
- <deny users="?" roles="user"/>
- </authorization>
- </system.web>
- </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è)面。
- private void Button1_Click(object sender, System.EventArgs e)
- {
- if(this.IsAuthenticated(TextBox1.Text,TextBox2.Text))
- {
- string userId = TextBox1.Text;
- FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
(1,userId,DateTime.Now,DateTime.Now.AddSeconds(30),false,roles);- HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket));- Response.Cookies.Add(cookie);
- Response.Redirect(FormsAuthentication.GetRedirectUrl(userId,false),true);
- }
- }
然后在項(xiàng)目中添加新類(lèi),繼承IHttpModule接口,實(shí)現(xiàn)Init 方法,并且注冊(cè)需要的方法 :
- namespace WebApplication1
- {
- /**//// <summary>
- /// Summary description for AuthenticationModule.
- /// </summary>
- public class AuthenticationModule : IHttpModule
- {
- public AuthenticationModule()
- {
- //
- // TODO: Add constructor logic here
- //
- }
- private void Authentication_Request(object sender,EventArgs e)
- {
- HttpApplication App = (HttpApplication) sender;
- HttpContext Ctx = App.Context ;
- if (Ctx.Request.IsAuthenticated == true)
- {
- FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;
- FormsAuthenticationTicket Ticket = Id.Ticket ;
- string[] Roles = Ticket.UserData.Split (',') ;
- Ctx.User = new GenericPrincipal (Id, Roles) ;
- }
- }
- IHttpModule Members#region IHttpModule Members
- void IHttpModule.Init(HttpApplication context)
- {
- context.AuthenticateRequest += new EventHandler(this.Authentication_Request);
- }
- void IHttpModule.Dispose()
- {
- }
- #endregion
- }
- }
在上面的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)下,添加如下代碼:
- <httpModules>
- <add name="AuthenticationModule" type="WebApplication1.
AuthenticationModule, WebApplication1" />- </httpModules>
大功告成,現(xiàn)在可以編譯通過(guò)后,將index1.aspx設(shè)置為起始頁(yè),運(yùn)行一下,是不是重定向到login.aspx頁(yè)面了?然后分別用user和admin登陸,看看效果。以上介紹ASP.NET的HttpModule
【編輯推薦】