首页 > 解决方案 > 在 ASP.NET 和经典 ASP 之间共享会话状态

问题描述

关于此主题的精彩文章:https ://searchwindevelopment.techtarget.com/tip/Share-session-state-between-ASP-and-ASPNET-apps

问题是,没有源代码我无法让它工作。文章中的代码片段显示了 Visual Studios 中的许多错误。作者 Dennis Hurst 无法访问,因为它是在 2004 年编写的。那里有人有他们可以发布的实际源代码吗?或者也许指出我正确的方向?我的目标是将经典 ASP 对象数据(应用程序)提取到共享同一文件夹的 ASP.Net 代码中。我已经读到使用 COMM Wrapper 可能是可能的,但这超出了我的技能水平。这听起来像是我的问题的最佳解决方案。预先感谢您的帮助。

// The constructor for this class takes a reference to the HttpContext and derives the URL it will need to send its requests to
    public ASPSessionVar(HttpContext oInContext)
    {
        oContext = oInContext;
        ASPSessionVarASP = "SessionVar.asp";
        /* We now build a System.Uri object to derive the correct
           URL to send the HTTP request to. oContext.Request.Url
           will contain a System.Uri object that represents
           this ASPXs URL.
        */
        System.Uri oURL = oContext.Request.Url;
        ASPSessionVarASP = oURL.Scheme + "://"
            + oURL.Host + ":" + oURL.Port.ToString()
            + ASPSessionVarASP;

    }


    //-------------------------------------------------------------------------//
    // The primary function for this example is called GetSessionVar. It does the majority of the work done by this application,
    // This includes creating a WebRequest, sending it off to the ASP page, and returning the response.


    // First get the Session Cookie
    string ASPCookieName = "";
    string ASPCookieValue = "";
 if (!GetSessionCookie
(out ASPCookieName, out ASPCookieValue))
 {
   return  "";
 }

 // Initialize the WebRequest.
 HttpWebRequest myRequest =
     (HttpWebRequest)WebRequest.Create
     (ASPSessionVarASP + "?SessionVar=" + ASPSessionVar);
       myRequest.Headers.Add
     ("Cookie: " + ASPCookieName + "=" + ASPCookieValue);

 // Send the request and get a response
 HttpWebResponse myResponse =
   (HttpWebResponse)myRequest.GetResponse();

    Stream receiveStream = myResponse.GetResponseStream();

    System.Text.Encoding encode =
        System.Text.Encoding.GetEncoding("utf-8");

    StreamReader readStream =
        new StreamReader(receiveStream, encode);

    string sResponse = readStream.ReadToEnd();

    // Do a bit of cleanup
    myResponse.Close();
 readStream.Close();
 return sResponse;
  }

//------------------------------------------------------------------------------------------------------------------------//
// This function simply takes the Request that was passed by the client and extracts the ASP Session cookie from it. 
// This function is called by the GetSessionVar function to retrieve the ASPSession cookie.

private bool GetSessionCookie
  (out string ASPCookieName, out string ASPCookieValue)
   {
       int loop1;
       HttpCookie myCookie;     // Cookie variable

    ASPCookieName = "";
    ASPCookieValue = "";

    // Capture all cookie names into a string array.
    String[] CookieArray =
        oContext.Request.Cookies.AllKeys;

    // Grab individual cookie objects by cookie name.
    for (loop1 = 0; loop1 < CookieArray.Length; loop1++)
    {
        myCookie =
          oContext.Request.Cookies[CookieArray[loop1]];
        if (myCookie.Name.StartsWith("ASPSESSION"))
        {
            ASPCookieName = myCookie.Name;
            ASPCookieValue = myCookie.Value;
            return true;
        }
    }

    return false;
}

//--------------------------------------------------------------------------------------------------------------------------------//
//The ASPX page will instantiate an ASPSessionVar object, passing in the current Context to the construct or.
//The GetSessionVar function is then called, passing in the name of the ASP Session variable that is to be retrieved.


//Create an ASPSessionVar object,
//passing in the current context
SPI.WebUtilities.ASP.ASPSessionVar oASPSessionVar
  = new SPI.WebUtilities.ASP.ASPSessionVar(Context);
string sTemp = oASPSessionVar.GetSessionVar("FirstName");


// CLASSIC ASP CODE BELOW !!
//The ASP code for this example was placed in an ASP file called SessionVar.asp. 
// It performs two simple tasks. First, it ensures that the request is coming from the server that the ASP page is running on. 
// This ensures that the request is valid and coming ONLY from the Web server's IP address. 
// The ASP page then returns the session variable it was asked to provide

<%
  dim sT
  if Request.ServerVariables("REMOTE_ADDR") =
   Request.ServerVariables("LOCAL_ADDR") then
   sT = Request("SessionVar")
   if trim(sT) <> "" then
     Response.Write Session(sT)
   end if
  end if
  %>

标签: asp.netasp-classic

解决方案


推荐阅读