473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do u set classic ASP session variables from an ASP.NET application???

My project currently requires that I integrate an ASP.NET application with
an ASP application. One of the issues I'm having is that I have some very
long strings being created in an ASP.NET application that need to somehow
appear in a classic ASP session. The strings are too long for cookies or
URLs and I'd like to avoid using a database or someother temporary server
storage.

So far what I've come up with is a .NET class that "spoofs" a post to an ASP
page specifically designed to load its post data into the session. The post
spoof involves a System.NET.HTTPWebRequest object, copying the HTTP headers
from HTTPContext.Current.Request, and creating a post data byte array.

What i've found so far is that even though it would appear my HTTPWebRequest
object has a cookie in its headers containing the ASP session identifier for
an already established ASP session, the request causes a new ASP session to
be created. Also, it appears that my post data isn't making it to the ASP
page that is responsible for mapping the post data into the ASP session.

Any advice or information would be greatly apreciated//

Below is the source code from my current class
================================================== ==========
/// <summary>
/// <c>ASPSession</c> is an adapter for moving data from the .NET runtime
to a traditional ASP session
/// This is accomplished by maintaining a local collection of name-value
pairs, then including these pairs in
/// a POST request to a traditional ASP page that is ultimately responsible
for committing the name-value pairs to an ASP session
/// ** this means that in order to function correctly, this class requires
the existence of the described receiver ASP page
///
/// Sample VBScript code for ASP receiver page
/// <%
///
/// for each formKey in Request.Form
/// if ( InStr( Upper( formKey ), "SESSIONKEY." ) ) then
/// session( Replace( Upper( formKey ), "SESSIONKEY.", "" ) ) =
Request.Form( formKey )
/// end if
/// next
///
/// %>
/// </summary>
public class ASPSession
{

#region Fields

private const String _ASP_RECEIVER_PAGE = "RuntimeToSessionReceiver.asp";
private const String _ASP_RECEIVER_CONTENTTYPE =
"application/x-www-form-urlencoded";
private const String _ASP_SESSIONKEY_POST_PREFIX = "SessionKey.";
private StringDictionary _aspSessionValues;
private HttpWebRequest _aspReceiverRequest;

#endregion

#region Constructors

/// <summary>
/// Creates a new instance of ASPSession
/// </summary>
public ASPSession()
{
_aspSessionValues = new StringDictionary();
configureASPReceiverRequest();
}

#endregion

#region Public Members

/// <summary>
/// Gets or Sets name-value pairs for submission to an existing ASP
session
/// </summary>
public String this[ String ASPSessionKeyName ]
{
get
{
if( _aspSessionValues.ContainsKey( ASPSessionKeyName ) )
{
return _aspSessionValues[ ASPSessionKeyName ];
}
else
{
return null;
}
}
set
{
if( _aspSessionValues.ContainsKey( ASPSessionKeyName ) )
{
_aspSessionValues[ ASPSessionKeyName ] = value;
}
else
{
_aspSessionValues.Add( ASPSessionKeyName, value );
}
}
}

/// <summary>
/// Executes an HTTP POST request containing the current collection of
name-value pairs to the ASP receiver page
/// * If it is determined that there are no name-value pairs to post, no
request is made.
/// </summary>
public void SetVars()
{
if( _aspSessionValues.Count > 0 )
{
setHeaders();
Byte[] postData = getPostData();
if( postData != null )
{
_aspReceiverRequest.ContentLength = postData.Length;
Stream sendStream = _aspReceiverRequest.GetRequestStream();
sendStream.Write( postData, 0, postData.Length );
sendStream.Close();
HttpWebResponse response = ( HttpWebResponse )
_aspReceiverRequest.GetResponse();
StreamReader reader = new StreamReader( response.GetResponseStream() );
String test = reader.ReadToEnd();
reader.Close();
}
}
}

#endregion

#region Private members

private void configureASPReceiverRequest()
{
String receiverURL;
if( String.Compare( HttpContext.Current.Request.ServerVariables[
"HTTPS" ], "on", true ) == 0 )
{
receiverURL = "https://";
}
else
{
receiverURL = "http://";
}
receiverURL += HttpContext.Current.Request.Url.Host + "/" +
_ASP_RECEIVER_PAGE;
_aspReceiverRequest = (
HttpWebRequest )System.Net.HttpWebRequest.Create( receiverURL );
_aspReceiverRequest.ContentType = _ASP_RECEIVER_CONTENTTYPE;
_aspReceiverRequest.Method = "POST";
}

private void setHeaders()
{
_aspReceiverRequest.Headers.Clear();
foreach( String headerName in HttpContext.Current.Request.Headers )
{
try
{
_aspReceiverRequest.Headers.Add( headerName,
HttpContext.Current.Request.Headers[ headerName ] );
}
catch( ArgumentException )
{
// Some headers are restricted (why, i'm not sure) and will cause an
ArgumentException to occur
}
}
}

private Byte[] getPostData()
{
String sessionKey;
String postDataString = String.Empty;
foreach( DictionaryEntry aspSessionEntry in _aspSessionValues )
{
sessionKey = aspSessionEntry.Key.ToString();
if( sessionKey.Length > 0 )
{
postDataString += String.Format(
"&{0}={1}",
HttpUtility.UrlEncode( String.Format( "{0}{1}",
_ASP_SESSIONKEY_POST_PREFIX, sessionKey ) ),
HttpUtility.UrlEncode( aspSessionEntry.Value.ToString() )
);
}
}
if( postDataString.Length == 0 ) return null;
postDataString = postDataString.Substring( 1 );
return Encoding.ASCII.GetBytes( postDataString );
}

#endregion

}
Nov 19 '05 #1
0 1797

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wynter | last post by:
I've been smacking into one brick wall after the next in converting some of my code from ASP Classic to ASP.NET. This one is a real bugger. I use Session Variables to help with User Security. If they...
4
by: Tony | last post by:
I have a classic ASP application that determines whether a user is logged in by examining a Session Variable, Session("LoginId"). Once logged in there is a link to a new search page (.aspx) this...
4
by: Chris Newby | last post by:
My project currently requires that I integrate an ASP.NET application with an ASP application. One of the issues I'm having is that I have some very long strings being created in an ASP.NET...
2
by: Wynter | last post by:
I've been smacking into one brick wall after the next in converting some of my code from ASP Classic to ASP.NET. This one is a real bugger. I use Session Variables to help with User Security. If they...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.