您的位置:首页 > 编程语言 > ASP

asp.net 跨网站/应用程序池/虚拟目录/域名 的Session共享解决方案

2008-01-29 08:22 489 查看
首先大概说明一下思路.在不同网站/域名/应用程序池或者是虚拟目录下的使用session是无法通用的.我们使用什么办法能使其通用? 下面我就给大家说一下简单的方法.

程序是C#的.大家可以用任意语言编写.
首先我们构造一个Form表单出来.里卖弄有各种字段.用来放你的用户信息.我在C#中使用的是封装类存储用户信这么一来在转换的时候需要做一些处理

代码

Go2Aspx.aspx页面
public static void session2aspx()
{

// 获得要跳转到的目录或者网址
string module = HttpContext.Current.Request.QueryString["m"];

//获得跳转网址或目录下某各文件
string tourl = HttpContext.Current.Request.QueryString["tourl"];

/*构造表单开始*/
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Write("<!DOCTYPE html PUBLIC /"-//W3C//DTD XHTML 1.1//EN/" /"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd/">" +
"<html xmlns=/"http://www.w3.org/1999/xhtml/"><head></head><body>");
foreach (string key in HttpContext.Current.Request.QueryString.AllKeys)
{

//循环所有传递参数
if (key == "m") continue;
if (key == "tourl") continue;
tourl += "&" + key + "=" + HttpContext.Current.Request.QueryString[key];
}
HttpContext.Current.Response.Write(@"<form name='session2aspx' id='session2aspx' action='"
+ module + "Session.aspx?tourl=" + tourl +
"' method='post'><input type='submit' style='display:none' />");

//循环得到所有session
foreach (string key in HttpContext.Current.Session.Keys)
{
HttpContext.Current.Response.Write("<input type='hidden' name='" + key + "' value='" + HttpContext.Current.Session[key] + "' />");
}

HttpContext.Current.Response.Write("</form>");
HttpContext.Current.Response.Write("<script language='javascript'>document.session2aspx.submit();</script>");
HttpContext.Current.Response.Write("</body></html>");
}

OK转换方法.页面名称为ASPX2ASPX.aspx下面是接受的方法
//处理域名
private static string SetUrlForMat(string strUrl)
{
string url=strUrl;
url = strUrl.Replace("http://", "");
if (url.Contains("/"))
{
url = "http://" + url.Substring(0, url.IndexOf("/"));
}
else
{
url = "http://" + strUrl;
}
return url;
}

Session.aspx
public static void session2me()
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

//获取当前域名
string host = "http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
//获取上页URL
string referer = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
if (referer == null) HttpContext.Current.Response.End();
//referer = referer.Substring(0, host.Length).ToUpper();
referer = SetUrlForMat(referer).ToUpper();
host = host.ToUpper();
//判断来源网址是否是同一服务器.或者是指定得服务器非常必要地验证
if (
referer == host ||
referer == "HTTP://LOCALHOST" ||
referer == "HTTP://BG1JT"
)
{
HttpContext.Current.Session.Clear();
foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
HttpContext.Current.Session[key] = HttpContext.Current.Request.Form[key].ToString();

}
}

string tourl = HttpContext.Current.Request.QueryString["tourl"];
foreach (string key in HttpContext.Current.Request.QueryString.AllKeys)
{
if (key == "m") continue;
if (key == "tourl") continue;
tourl += "&" + key + "=" + HttpContext.Current.Request.QueryString[key];
}

if (tourl != null)
{
HttpContext.Current.Response.Redirect(tourl);
}
else
{
HttpContext.Current.Response.Redirect("index.aspx?");
}
}

转换完毕...这样一个虚拟目录得Session 就可以转到另一个虚拟目录下.包括域名之间....
这样的Session转换同样支持封装类.我们也可把用户信息封装后转换.转换时只是需要把相关的东东序列化..以及反序列化.
这样转换最好有比较严格的验证.比如加密等.我上面只是做了一个小小的示例.同样的方法可在任意语言编写的网站中共享Session
使用方法
http://localhost/Go2Aspx.aspx?m=http://bg1jt/&tourl=blog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: