ASP 到 ASP.NET 会话变量

ASP to ASP.NET Session Variables(ASP 到 ASP.NET 会话变量)
本文介绍了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 会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
    InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
    how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
    Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
    Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
    How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)