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

41 篇文章 0 订阅
8 篇文章 0 订阅

 首先大概说明一下思路.在不同网站/域名/应用程序池或者是虚拟目录下的使用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
使用方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值