码迷,mamicode.com
首页 > Web开发 > 详细

基于HttpModule扩展

时间:2014-11-12 11:59:27      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:httpmodule   httphandler   

bubuko.com,布布扣

一般处理页面就是HttpHandler区域

-------------------------------封装类库

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
/*
 **************************
 * 案例功能:
 * 1,URL地址栏阻止(参数为aspx则跳转到错误页面)
 * 2,Form表达阻止(表单的值为aspx则弹出错误)
 * 3,阻止使用session
 **************************
 */
namespace HttpModuleDome
{
    public class MyHttpModule : IHttpModule
    {
        #region IHttpModule 成员
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }
        //开始请求阶段
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            //Url地址栏阻止
            if (context.Request.QueryString.Count > 0)
            {
                for (int i = 0; i < context.Request.QueryString.Count; i++)
                {
                    if (context.Request.QueryString[context.Request.QueryString.Keys[i]] == "aspx")
                    {
                        context.Response.Redirect("http://www.baidu.com");
                        context.Response.End();
                    }
                }
            }
            //Form表单阻止
            if (context.Request.Form.Count > 0)
            {
                for (int i = 0; i < context.Request.Form.Count; i++)
                {
                    if (context.Request.Form[context.Request.Form.Keys[i]] == "aspx")
                    {
                        context.Response.Write("<script>alert(‘错误‘);location.href=‘" + context.Request.RawUrl + "‘</script>");
                        context.Response.End();
                    }
                } 
            }
        }
        //进入了HttpHandler区域,已经有了session
        void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;//Global.asax的基类
            HttpContext context = application.Context;//封装了ASP.NET要处理的单次请求的所有信息
            if (context.Session.Count > 0)
            {
                //context.Response.End();//直接跳过AcquireRequestState之后的请求,结束请求
                
            }
        }
        #endregion
    }
}

 ------------------------------------web.config里面引用

 <system.web>  
 <httpModules>

   <add name="MyhttpModule" type="HttpModuleDome.MyHttpModule,HttpModuleDome"/>

  </httpModules>

 </system.web>

 ------------------------------------也可以在Global.asax文件里面写

<%@ Application Language="C#" %>
<script runat="server">
    /*
     *格式:以Application_开头
     */
    //开始请求阶段
    void Application_BeginRequest(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
    }
    
    
    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码
    }
    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码
    }
    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。
    }
       
</script>

bubuko.com,布布扣

bubuko.com,布布扣

本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1575612

基于HttpModule扩展

标签:httpmodule   httphandler   

原文地址:http://962410314.blog.51cto.com/7563109/1575612

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!