Passing session values from aspx to asp

时间:2016-04-04 17:36:39

标签: c# asp.net

I am trying to pass a user name value from asp.net to classic asp application. But when the ASP page come out I do not see any value. If there is any good link to pass session variables please post here. I tried multiple ones but not getting success.

 public static object Get(string name)
    {
        HttpContext context = HttpContext.Current;
        object value = null;
        String[] cookies = context.Request.Cookies.AllKeys;
        for (int i = 0; i < cookies.Length; i++)
        {
            HttpCookie cookie = context.Request.Cookies[cookies[i]];
            if (cookie.Name.StartsWith("ASPSESSION"))
            {
                System.Uri uri = context.Request.Url;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp?mode=get&name=" + name);
                request.Headers.Add("Cookie: " + cookie.Name + "=" + cookie.Value);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader readStream = new StreamReader(responseStream, encode);
                value = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
                break;
            }
        }
        return value;
    }

    public static void Set(string name, object value)
    {
        HttpContext context = HttpContext.Current;

        String[] cookies = context.Request.Cookies.AllKeys;

        for (int i = 0; i < cookies.Length; i++)
        {
            HttpCookie cookie = context.Request.Cookies[cookies[i]];

            if (cookie.Name.StartsWith("ASPSESSION"))
            {
                System.Uri uri = context.Request.Url;


                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp?mode=set&name=" + context.Server.UrlEncode(name) + "&value=" + context.Server.UrlEncode(value.ToString()));

                request.Headers.Add("Cookie: " + cookie.Name + "=" + cookie.Value);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                break;
            }
        }
    }

    protected void btnSEssion_Click(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        System.Uri uri = context.Request.Url;
        //Get("UserName");
        Set("UserName", UserName);
        Response.Redirect(uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp");
    }

ASP page

Response.Write("This is FinalPage.asp<BR>")
Response.Write "Shared Session Variable Names/Values between ASP.NET and     classic ASP:<BR>"


Dim strMode, strName, strValue
Response.Write "1<BR>"

If Request.ServerVariables("REMOTE_ADDR") =     Request.ServerVariables("LOCAL_ADDR") Then
Response.Write "2<BR>"
strMode = Request.QueryString("mode")
Response.Write strMode
Response.Write "3<BR>"
strName = Request.QueryString("name")
Response.Write strName
Response.Write "4<BR>"
If strMode = "get" Then
    Response.Write(Session(strName))
ElseIf strMode = "set" Then
    strValue = Request.QueryString("value")
    Session(strName) = strValue
End If
End If
%>

1 个答案:

答案 0 :(得分:2)

Separate applications don't share session state. (Just imagine what it would mean for security if you could read the session state of any application running on the same server.)

In order to pass values to a URL in a GET request (which is what a redirect performs), those values would have to be on the query string. Something like this:

Response.Redirect(
    uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp"
    + "?someKey=" + HttpUtility.UrlEncode(someValue)
    + "&anotherKey=" + HttpUtility.UrlEncode(anotherValue));
    // and so on for any additional values

It's not entirely clear to me from the current code what the someKey and someValue items should be. But whatever those key/value pairs are or wherever you get them, this is the structure to include them in the redirect.


Edit: If the values are in some way sensitive and shouldn't be exposed outside the server-side code then the alternative is to pass a non-sensitive (de-identified) value which corresponds to the server-side stored data.

Basically, store the data in a database which both applications can access. When writing that record, you'd have some sort of identifier to later read that record (maybe a auto-generated ID from the database, maybe a GUID you generated in code, maybe something else, it's up to you). That identifier would be passed along on the query string to the next application, which would use that identifier to fetch the record from the database.

Without going into details on how to read/write from a database (assuming you have that covered), the semantic structure would be something like:

var recordID = WriteToSharedDB(someValue, anotherValue);
Response.Redirect(
    uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp"
    + "?id=" + HttpUtility.UrlEncode(recordID);

Then in the target application (Classic ASP in this case) you'd get the id from the query string and use it to select the data from the database.