ASP到ASP.NET会话变量

Psy*_*UCK 10 asp.net session asp-classic

我们有一个经典asp的网站,我们正在慢慢迁移到ASP.NET.

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

是否有最佳实践将会话变量传递给经典的asp和asp.net?安全是必须的,并且非常感谢任何带有示例的解释.

Aar*_*k71 6

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

  • 在ASP端:输出会话的asp页面,称之为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
    %>
    
    Run Code Online (Sandbox Code Playgroud)
  • 在.net端,远程调用该asp页面.:

    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;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)