C# 将会话值从aspx传递到asp
我正在尝试将用户名值从asp.net传递到经典asp应用程序。但是当ASP页面出来时,我看不到任何价值。如果有任何传递会话变量的好链接,请在这里发布。我尝试了多次,但都没有成功C# 将会话值从aspx传递到asp,c#,asp.net,C#,Asp.net,我正在尝试将用户名值从asp.net传递到经典asp应用程序。但是当ASP页面出来时,我看不到任何价值。如果有任何传递会话变量的好链接,请在这里发布。我尝试了多次,但都没有成功 public static object Get(string name) { HttpContext context = HttpContext.Current; object value = null; String[] cookies = context.R
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");
}
公共静态对象Get(字符串名称)
{
HttpContext=HttpContext.Current;
对象值=空;
String[]cookies=context.Request.cookies.allkey;
for(int i=0;i
ASP页面
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
%>
Response.Write(“这是FinalPage.asp
”)
响应。写入“ASP.NET和经典ASP之间的共享会话变量名称/值:
”
暗标准模式、标准名称、标准值
回答。写“1
”
如果Request.ServerVariables(“REMOTE\u ADDR”)=Request.ServerVariables(“LOCAL\u ADDR”),则
回答。写“2
”
strMode=Request.QueryString(“模式”)
响应。写strMode
回答。写“3
”
strName=Request.QueryString(“名称”)
回复。写strName
回答。写“4
”
如果strMode=“get”,则
响应.写入(会话(strName))
ElseIf strMode=“set”然后
strValue=Request.QueryString(“值”)
会话(strName)=strValue
如果结束
如果结束
%>
单独的应用程序不共享会话状态。(想象一下,如果您可以读取在同一服务器上运行的任何应用程序的会话状态,那么这对安全性意味着什么。)
为了在GET请求中将值传递给URL(这是重定向执行的),这些值必须位于查询字符串上。大概是这样的:
Response.Redirect(
uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp"
+ "?someKey=" + HttpUtility.UrlEncode(someValue)
+ "&anotherKey=" + HttpUtility.UrlEncode(anotherValue));
// and so on for any additional values
从当前代码来看,我并不完全清楚someKey
和someValue
项应该是什么。但是,不管这些键/值对是什么,或者在哪里得到它们,这就是将它们包含在重定向中的结构
编辑:如果这些值在某种程度上是敏感的,并且不应该暴露在服务器端代码之外,那么替代方法是传递一个与服务器端存储数据相对应的非敏感(取消标识)值 基本上,将数据存储在两个应用程序都可以访问的数据库中。在编写该记录时,您将拥有某种标识符,以便以后读取该记录(可能是数据库中自动生成的ID,可能是代码中生成的GUID,可能是其他内容,由您决定)。该标识符将通过查询字符串传递给下一个应用程序,该应用程序将使用该标识符从数据库中获取记录 不必详细介绍如何从数据库读/写(假设您已经介绍过),语义结构如下:
var recordID = WriteToSharedDB(someValue, anotherValue);
Response.Redirect(
uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp"
+ "?id=" + HttpUtility.UrlEncode(recordID);
然后在目标应用程序(本例中为经典ASP)中,您将从查询字符串中获取
id
,并使用它从数据库中选择数据。单独的应用程序不共享会话状态。(想象一下,如果您可以读取在同一服务器上运行的任何应用程序的会话状态,那么这对安全性意味着什么。)
为了在GET请求中将值传递给URL(这是重定向执行的),这些值必须位于查询字符串上。大概是这样的:
Response.Redirect(
uri.Scheme + "://" + uri.Host + ":" + "5500" + "/FinalPage.asp"
+ "?someKey=" + HttpUtility.UrlEncode(someValue)
+ "&anotherKey=" + HttpUtility.UrlEncode(anotherValue));
// and so on for any additional values
从当前代码来看,我并不完全清楚someKey
和someValue
项应该是什么。但是,不管这些键/值对是什么,或者在哪里得到它们,这就是将它们包含在重定向中的结构
编辑:如果这些值在某种程度上是敏感的,并且不应该暴露在服务器端代码之外,那么替代方法是传递一个与服务器端存储数据相对应的非敏感(取消标识)值 基本上,将数据存储在