asp.net与asp的session共享 及 asp的请求拦截

时间:2022-04-23
本文章向大家介绍asp.net与asp的session共享 及 asp的请求拦截,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

asp.net 与 asp 的session是无法直接共享的(底层的处理dll也不一样),要想互通session,只能用变通的办法:

一、asp.net -> asp 的session传递

a) 建一个类似SessionHanler.asp的页面,代码如下:

<!-- #include virtual="inc/Func.asp" -->
<%
    Dim returnUrl
    Session("user") = Request("user")
    Set returnUrl = Request("returnUrl")
    if returnUrl="" then
        returnUrl = "home.asp"
    end if    
    
    '记日志
    AppendFile "/log/log.txt" , Now() & ",user:"&Session("user") &",returnUrl:" & returnUrl

    Response.Redirect(returnUrl)
%>

 大概功能,就是接收参数,然后按需要生成Session,最后重定向到真正的功能页面,这样其它页面访问Session时就有值了

b) 建一个asp.net页面,把需要传递的值,以参数形式提交到 SessionHanler.asp  (POST或GET方式都行),参考代码:

using System;

namespace ASP_ASPX_TEST
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string returnUrl = "home.asp";
                string sessoinHandlerUrl = "SessionHandler.asp";
                string postData = "user=" + this.txtUser.Text + "&returnUrl=" + returnUrl;
                Response.Redirect(sessoinHandlerUrl + "?" + postData);
            }
        }
    }
}

 二、asp -> asp.net 的session传递

反过来做即可,原理完全相同。

三、拦截asp请求

对于现有的asp项目,在不修改其asp源代码的前提下,如果需要对http请求加以拦截(例如:把拦截到的请求参数做些处理,再转发到其它子系统。同时不影响原来asp项目的正常运行),有二种做法:

a) 自行开发ISAPI 筛选器 ,然后在IIS里,把自己开发的dll加入ISAPI 筛选器

这个方法比较繁琐,技术难度也相对较高,在今天.net的时代,不推荐大家使用,有兴趣的可以参考一个开源项目: http://filterdotnet.codeplex.com/

以及 一些ISAPI的开发文章,比如

ISAPI开发介绍 http://blog.csdn.net/mycoolx/article/details/6913048

ISAPI tutorial for Delphi developers http://delphi.about.com/library/bluc/text/uc060901c.htm

delphi IIS ISAPI http://www.cnblogs.com/cisky/archive/2011/01/05/1926249.html

delphi IIS ISAPI  http://siyebocai.blog.163.com/blog/static/103316426200810297512408/

用Delphi编写 IIS ISAPI 程序 http://download.csdn.net/detail/wwwvvingnet/2229146

在Delphi中用IIS或PWS调试ISAPI程序 http://bbs.csdn.net/topics/7979

b) 利用asp.net的HttpModule (环境:IIS7 /Asp.Net 4.0上 测试通过)

前提:Asp项目所用的应用程序池必须采用"集成"模式

先建一个HttpModule

using System;
using System.IO;
using System.Web;

namespace ASP_ASPX_TEST
{
    public class MyHttpModule : IHttpModule
    {
        string logFileName;

        public MyHttpModule()
        {
            logFileName = HttpContext.Current.Server.MapPath("~/log/request.log");
        }

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += BeginRequestHandle;
            context.EndRequest += EndRequestHandle;

        }

        private void BeginRequestHandle(Object source, EventArgs e)
        {

            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            HttpRequest request = context.Request;

            File.AppendAllText(logFileName,Environment.NewLine + request.Url + Environment.NewLine + "BeginRequest => " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
            foreach (var item in request.QueryString.AllKeys)
            {
                File.AppendAllText(logFileName, string.Format("  QueryString => {0}:{1}", item, request.QueryString[item]) + Environment.NewLine);
            }

            foreach (var item in request.Form.AllKeys)
            {
                File.AppendAllText(logFileName, string.Format("  Form => {0}:{1}", item, request.Form[item]) + Environment.NewLine);
            }
            context.Response.Write("BeginRequestHandle<br/>");

        }

        private void EndRequestHandle(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            HttpRequest request = context.Request;

            File.AppendAllText(logFileName, Environment.NewLine + request.Url + Environment.NewLine + "EndRequest => " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine);
            foreach (var item in request.QueryString.AllKeys)
            {
                File.AppendAllText(logFileName, string.Format("  QueryString => {0}:{1}", item, request.QueryString[item]) + Environment.NewLine);
            }

            foreach (var item in request.Form.AllKeys)
            {
                File.AppendAllText(logFileName, string.Format("  Form => {0}:{1}", item, request.Form[item]) + Environment.NewLine);
            }
            context.Response.Write("EndRequestHandle<br/>");
        }
    }
}

 这里只是演示代码,我把所有请求的QueryString和Form参数都记录了下来。

web.config中修改配置

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="MyHttpModule" type="ASP_ASPX_TEST.MyHttpModule,ASP_ASPX_TEST" />
    </modules>
  </system.webServer>

</configuration>

 注:IIS7及以上版本,自定义的HttpModule,必须加到 system.webServer节点下,否则只能拦截asp.net的请求,对asp无效

最后赠送一个asp调试的小技巧(自从asp.net出来以后,很多人估计象我一样,已经很久不碰asp,这些小技巧差不多忘记光了,贴在这里备份一下)

IE浏览器里先去掉 友好错误的勾选

IIS设置里,允许发送详细错误到客户端

这样,asp代码出错时,就会显示详细信息了