Share ASP.NET Context with ASP 2 & 3 – level 400

We all have ASP applications that we need to extend with ASP.NET.  Unless our functionality is wrapped in COM, we may have to rewrite some in .Net.  Wouldn’t it be nice if we could rewrite common app functionality in .Net and then reuse it in the ASP?  I’ve worked up a sample that does just that.  I have a web service that exposes the Session object, so now ASP can query the ASP.NET’s Session object (this method could also be used for the Cache object, Application, etc.).  For this to work, ASP has to be in the same ASP.NET session.  That means that the cookie “ASP.NET_SessionId” must exist on the ASP and ASP.NET side, and it must contain the same session id.  Using the Soap Toolkit v.3, I make the web service calls.  One of the web service methods returns the ASP.NET SessionID.  I use this to manually make a Set-Cookie header in ASP because ASP natively HTML Encodes cookie names (would result in %20, or whatever for the . and the _).  Then in subsequent web service calls, I add a soap header to include the cookie with the ASP.NET session id, and this allows every web service call to participate in the same session, so all the data remains coherant. 


Here is my ASP code:


<%@ Language=VBScript%>



<html>



<body>



<%



set soapclient = CreateObject(“MSSOAP.SoapClient30”)



On Error Resume Next



Call soapclient.mssoapinit(Server.MapPath(“Math.wsdl”))



if err <> 0 then



  Response.Write “initialization failed” + err.description



end if



Dim strASPNETSession



if(Request.Cookies(“ASP.NET_SessionID”) <> “”) Then



      soapclient.ConnectorProperty(“RequestHTTPHeader”) = “COOKIE:” & Request.ServerVariables(“HTTP_COOKIE”)



Else



      strASPNETSession = soapclient.GetASPNET_SessionID()



      soapclient.ConnectorProperty(“RequestHTTPHeader”) = “COOKIE:ASP.NET_SessionID=” & strASPNETSession



End If




Dim a



a = soapclient.GetCookies()



Call Response.AddHeader(“Set-Cookie”, a)



Response.Write(“HTTP_COOKIE Server Variables: “)



Response.Write(Request.ServerVariables(“HTTP_COOKIE”) & “<br><br>”)




Response.Write(“Session Hit Counter = ” & soapclient.SessionHitCounter())




if err <> 0 then



  Response.Write err.description & “<BR/>”



  Response.Write “faultcode=” + soapclient.faultcode & “<BR/>”



  Response.Write “faultstring=” + soapclient.faultstring & “<BR/>”



  Response.Write “FaultActor=” + soapclient.FaultActor & “<BR/>”



  Response.Write “Detail=” + soapclient.Detail & “<BR/>”



end if



%>



</body>



</html>



And my web service code:


[WebMethod(true)]



            public string GetSession(string key)



            {



                  return (string)Session[key];



            }



            [WebMethod(true)]



            public void SetSession(string key, string myValue)



            {



                  HttpContext.Current.Session[key] = myValue;



            }



            [WebMethod(true)]



            public int SessionHitCounter()



            {



                 




                  if (Session[“HitCounter”] == null)



                  {



                        Session[“HitCounter”] = 1;



                  }



                  else



                  {



                        Session[“HitCounter”] = ((int) Session[“HitCounter”]) + 1;



                  }



                  return ((int) Session[“HitCounter”]);



            }



            [WebMethod(true)]



            public string GetASPNET_SessionID()



            {



                  return Context.Request.Cookies[“ASP.NET_SessionId”].Value;



            }



            [WebMethod(true)]



            public string GetCookies()



            {



                  return Context.Request.ServerVariables[“HTTP_COOKIE”];



            }



I was so excited when I got this to work, I had to share.  If you search for this topic on Google, there are thousands of people who have this problem.