zoukankan      html  css  js  c++  java
  • 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代码出错时,就会显示详细信息了

  • 相关阅读:
    php生成随机颜色代码
    终于完成了 源码 编译lnmp环境
    json_encode 中文 null
    push is not a function
    linux 搭建svn 服务器
    samba 挂载windows共享文件夹
    php 加密 解密 方法
    serialize unserialize
    CentOS Linux解决Device eth0 does not seem to be present 但是没有发现eth1
    javascript 获取视口的高度和宽度
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/3105320.html
Copyright © 2011-2022 走看看