Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.


Posted 06-01-2004 12:53 PM by Jeffrey Palermo

[Advertisement]

Comments

Chris Kinsman wrote re: Share ASP.NET Context with ASP 2 & 3 - level 400
on 06-05-2004 5:51 PM
Fairly inefficient and unless you change the connection limit it limits you to two connections to the web service at any point in time...
Jeffrey Palermo wrote re: Share ASP.NET Context with ASP 2 & 3 - level 400
on 06-06-2004 6:46 AM
To be honest, I hadn't thought about the connection limit. I'm still searching for the best way to share sessions with ASP and ASP.NET.
Jeffrey Palermo wrote re: Share ASP.NET Context with ASP 2 & 3 - level 400
on 06-08-2004 2:26 AM
The method I described above is a good way to make web service calls participate in the same session. This method does not scale for complete Session sharing if every hit of an ASP app is making a web service call. For high traffic like that, I think putting session information in sql server is a better option. And if all you need is the currently logged on user, then adding a cookie with the username is a good option.