例子如下
ASP应用URL为http://127.0.0.1/asp/,并在ASP.NET应用中的web.config设定
<!--设定ASP应用的URL-->
<add key="aspURL" value="http://127.0.0.1/asp/" />
在ASP应用中增加两个ASP页面system.asp和autoPostForm.asp
<!--system.asp-->
<%
Session("UID")="user"
session("isPass")="ok"
Server.Transfer("autoPostForm.asp")
%>
<!--autoPostForm.asp-->
<%
Response.Write("<form name=t id=t action=""http://127.0.0.1/aspdotnet/getSession.aspx""
method=post >")
Response.Write("<input type=hidden name=UID" )
Response.Write( " value=" & Session("UID") & " >")
Response.Write("<input type=hidden name=isPass" )
Response.Write( " value=" & Session("isPass") & " >")
Response.Write("</form>")
Response.Write("<script>t.submit();</script>")
%>
在ASP.net应用中用页面getSession.aspx来接受传递过来的Session变量值
getSession.aspx.cs代码片段:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string aspurl=ConfigurationSettings.AppSettings["aspURL"].Trim();
try
{
string fromurl=Request.ServerVariables["HTTP_REFERER"];
//验证是否从asp应用中提交过来
if(fromurl.StartsWith(aspurl))
{
string uid=Request["UID"].ToString();
string state=Request["isPass"].ToString();
if(uid!="" && state=="ok")
{
//表明用户在asp系统中已登录成功
}
}
else
{
Response.Write("<script>alert('非法用户或未登录用户');top.location.href='" + aspurl +
"';</script>");
}
}
catch
{
Response.Redirect(aspurl);
}
}
}
当然,上述例子只是为解决特定的问题,如果要写成通用的,则需要做如下修改
就在autoPostForm.asp使用
For each sItem in Session.Contents
Response.Write("<input type=hidden name=" & sItem)
Response.Write( " value=" & Session.Contents(sitem) & " >")
next
而在getSession.aspx页面用下面的代码来接受并用同名Session变量保存
for(int i=0;i<Request.Form.Count;i++)
{
Session[Request.Form.GetKey(i)]=Request.Form[i].ToString();
}