Share Session State Between ASP and ASP.NET Apps
Learn how — and how not — to share Session variables between your old and new apps.
October 30, 2009
asp:feature
LANGUAGE: C# | VBScript
ASP.NET VERSIONS: 1.0 | 1.1
Share Session State Between ASP and ASP.NET Apps
Learn how - and how not - to share Session variablesbetween your old and new apps.
By Dennis Hurst
As you migrate your Web apps from classic ASP to ASP.NET,you may not have the luxury of completely rewriting the entire applicationbefore going to production. Instead, you may need to migrate portions of the application from ASP toASP.NET, while leaving others as-is. If you need to make this type ofmigration, you will inevitably encounter the issue of how to integrate sessionstate from your ASP application into your ASP.NET app.
Unfortunately, the session management services for eachtechnology are completely separate, and there is no standard mechanism forsharing information between an ASP and an ASP.NET session. This article,however, shows a secure and simple method of sharing Session variables betweenASP and ASP.NET pages. Along the way, you'll also learn about severalalternative methods that are notsecure.
Must-Haves andMust-Nots
Any solution to the ASP/ASPX session sharing issue needsto address certain issues. First, it must be secure. Given that highlysensitive information is typically kept in session variables, security must beconsidered. A breach in a session sharing mechanism could result in a majorsystem breach.
Next, it should be elegant. A complex solution or one thatis difficult to maintain would be counterproductive. After all, this solutionis most likely a temporary solution until all of an application can be migratedto ASP.NET pages.
Finally, it should require minimal server-based componentsbeyond ASP.NET and ASP-based pages (or better yet, none at all). Many sites arehosted on remote servers; getting a vendor to run your DLL or other system filecan be challenging.
Typically, information stored in a Session variable is forinternal application use only and should be considered to be highly secure.Things that are often stored in the Session object include login state, userinformation, system information, and many potentially other secure items thatan end user should never see.Given the secure nature of information that is stored in a Session object,security must be the paramount concern. With that in mind, some of the obvioussolutions to the Session sharing issue must be avoided because they areextremely insecure. You should neverput sensitive information in a cookie, a hidden parameter, or a URL; and youshould avoid returning secure orsystem information to a browser, unless it's absolutely necessary.
The Big Question
HTTP is a stateless protocol. A request is sent by theclient to the server and a response is returned to the client. The connectionis then completed. The nature of this sessionless protocol has led vendors(like Microsoft) to come up with alternative means of maintaining session stateoutside the base HTTP protocol. In general, session state is maintained by ASPvia the use of a cookie that is sent from the server to the client. If you lookat the HTTP response, you'll see something like this:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0 Date:Mon,07 Apr 2003 12:52:26GMT
Content-Length: 10225
Content-Type: text/html
Cache-control: private
Set-Cookie: ASPSESSIONIDCSCRRCBS=GODPKFJDPJNMHGGJDOEIDDMK;
Note that the Web server (IIS) set a cookie with a namethat starts with ASPSESSION and has some seemingly random value. This is theASP Session cookie. All subsequent requests sent from the client to the serverwill include the ASP Session cookie along with the request. This cookie allowsIIS to associate the request with a specific session object that is stored onthe server.
ASP.NET uses the same technique to maintain session state,except that a different cookie name is used. In the case where an ASP sessionand an ASP.NET session have been established with the same server, the browserwill send both the ASP and ASP.NET cookies with each request. The request willlook something like this:
GET /MixedSessions/ASPSession.aspxHTTP/1.0
...headers removed to simplify example...
Cookie: ASPSESSIONIDAAR=NGHNLJKBBJG;
ASP.NET_SessionId=q5ydd3t45....
From this we know that if you have ASP and ASP.NETapplications running in the same folder (specifically if you put the ASP andASP.NET files in the same folder or virtual directory), the user will have aSession object in both the ASP and ASP.NET environments. The question nowbecomes how you can share information in the ASP environment with the ASP.NETenvironment.
All Together Now
The goal of this solution is to allow an ASP.NET app toretrieve variables from an ASP Session object. Figure 1 illustrates the flow ofthe process that occurs:
A user's browser will send a request from the client to the server. This request will contain the ASP and ASPX Session tokens (cookies).
An ASPX Web page that needs information from an ASP Session object will create an HTTP request and send it to an ASP page that is specifically designed to return ASP Session object variables securely.
The ASP page will authenticate that the request is from a local ASP.NET page only. Then it will look up the requested Session variable and return it in the HTTP response.
The ASP.NET page will do whatever processing is needed and generate the response.
The response is returned to the user.
Figure 1. By using a .NET class thatcreates a request containing the ASP Session cookie and sends it to an ASP pageto retrieve the ASP Session variable and an ASP page that authenticates therequest and returns the Session variable, you can share Session state betweenASP.NET and ASP apps.
In Step 2 above, the ASP.NET page will craft a requestthat contains the ASP Session cookie that was passed to it. This will allow theASP page to associate the request that comes from the ASPX page with the properuser's session.
Two components will be needed to make this system work.The first is a .NET class that will create a request that contains the ASPSession cookie and sends it to an ASP page to retrieve the ASP Sessionvariable. The second will be the ASP page that authenticates the request andthen returns the Session variable.
The .NET code consists of two main functions and aconstructor. The two functions work together to request Session informationfrom an ASP page and the constructor decides to which ASP page the necessaryrequest will be sent.
The constructor for this class takes a reference to theHttpContext and derives the URL it will need to send its requests to, as shownin Figure 2.
public ASPSessionVar(HttpContext oInContext)
{
oContext = oInContext;
ASPSessionVarASP ="SessionVar.asp";
/* We now build aSystem.Uri object to derive the correct
URL to send the HTTPrequest to. oContext.Request.Url
will contain aSystem.Uri object that represents
this ASPXs URL.