ASP 到 ASP.NET 会话变量 [英] ASP to ASP.NET Session Variables

查看:21
本文介绍了ASP 到 ASP.NET 会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个经典 ASP 网站,我们正在根据需要慢慢迁移到 ASP.NET.

We have a website in classic asp that we are slowly migrating to ASP.NET as necessary.

问题当然是经典的 asp 和 ASP.NET 是如何处理 Sessions 的.在花了最后几个小时研究网络之后,我发现了很多文章,但没有一篇比其他文章更突出.

The problem of course is how classic asp and ASP.NET handle Sessions. After spending the last few hours researching the web, I found many articles, but not one that stood out over the others.

是否有在经典 asp 和 asp.net 之间传输会话变量的最佳实践?安全性是必须的,非常感谢任何带有示例的解释.

Is there a best practice for transferring session variables from and to classic asp and asp.net? Security is a must and any explanation with examples is much appreciated.

推荐答案

将单个会话变量从经典 asp 传递到 .net 服务器端(对客户端隐藏会话值)的简单桥接器是这样的:

A simple bridge to pass a single session variable from classic asp to .net serverside (hiding your sessionvalue from the client), would be this:

  • 在 ASP 端:输出会话的 asp 页面,例如调用它.asp2netbridge.asp

  • On the ASP end: An asp page to output your session, call it e.g. asp2netbridge.asp

<%
'Make sure it can be only called from local server '
if (request.servervariables("LOCAL_ADDR") = request.servervariables("REMOTE_ADDR")) then
    if (Request.QueryString("sessVar") <> "") then
        response.write Session(Request.QueryString("sessVar"))
    end if
end if
%>

  • 在 .net 端,远程调用该 asp 页面.:

  • On the .net end, a remote call to that asp page. :

    private static string GetAspSession(string sessionValue)
     {
        HttpWebRequest _myRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://yourdomain.com/asp2netbridge.asp?sessVar=" + sessionValue));
        _myRequest.ContentType = "text/html";
        _myRequest.Credentials = CredentialCache.DefaultCredentials;
        if (_myRequest.CookieContainer == null)
            _myRequest.CookieContainer = new CookieContainer();
        foreach (string cookieKey in HttpContext.Current.Request.Cookies.Keys)
        {
            ' it is absolutely necessary to pass the ASPSESSIONID cookie or you will start a new session ! '
            if (cookieKey.StartsWith("ASPSESSIONID")) {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieKey.ToString()];
                _myRequest.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, string.IsNullOrEmpty(cookie.Domain)
                    ? HttpContext.Current.Request.Url.Host
                    : cookie.Domain));
            }
        }
        try
        {
            HttpWebResponse _myWebResponse = (HttpWebResponse)_myRequest.GetResponse();
    
            StreamReader sr = new StreamReader(_myWebResponse.GetResponseStream());
            return sr.ReadToEnd();
        }
        catch (WebException we)
        {
            return we.Message;
        }
    }
    

  • 这篇关于ASP 到 ASP.NET 会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆