如何Session实现跨站
在一台服务器上有个Session!从一个站点跳转到另一个站点时要怎么操作才有效果的!Session的值依然存在的???
比如:www.abc.com跳到www.bcd.com 这两个网站是在同一个服务器上的... --------------------编程问答-------------------- 其实Session能不能跨域要取决去Cookie能不能跨域。
先看Cookie 跨域吧。
CookieContainer.GetCookies
CookieContainer cookieContainer = new CookieContainer();
req.CookieContainer = cookieContainer;
参考
--------------------编程问答-------------------- 帮忙顶一下!
跨站session值保存的问题,确实不太好处理~~~每一次都是提示被application flushed,不懂怎么回事 --------------------编程问答-------------------- Error Go Into Particulars:
Class Name:JuyTang.Web.Global
Member Name:Application_Error
ErrorName:SaveSessionID
ErrorMessage:Session state has created a session id, but cannot save it because the response was already flushed by the application.
ErrorString: at System.Web.SessionState.SessionIDManager.SaveSessionID(HttpContext context, String id, Boolean& redirected, Boolean& cookieAdded)
at System.Web.SessionState.SessionStateModule.CreateSessionId()
at System.Web.SessionState.SessionStateModule.DelayedGetSessionId()
at System.Web.SessionState.SessionStateModule.ReleaseStateGetSessionID()
at System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs)
at System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
现在报错的内容!跪求高手!明再补100分!!!!! --------------------编程问答-------------------- session不能跨站吧
第一个网站把需要传的数据存到DB里,第2个网站取数据,新建session.这样做比较简单 --------------------编程问答-------------------- 利用序列化机制实现Session共享的原理:
1、Web Server 1的应用程序序列化Session信息为文本值(可以是Binary或Soap格式)
2、将序列化后的值写入文件,保存到File Server上
3、Web Server 2 对保存在File Server上的Session序列化后的值进行反序列化
4、在Web Server 2上重新构建Session值
下面我们来详细看看实现代码,分以下几个步骤:
1、创建一个类库工程:ShareSession
引入以下的命名空间:
System.configuration
System.Runtime.Serialization.Formatters.Soap
System.Web
2、创建一个类:SessionEntity
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ShareSession
...{
/**//* *****************************
*
* Author : Xiaojun Liu
* Create Date : 2007.11.09
* Description :
*
* *****************************
*/
[Serializable]
public class SessionEntity
...{
[NonSerialized]
public string AUTH_GUID = "";
private Hashtable _HtShareSession;
public Hashtable HtShareSession
...{
get
...{
return this._HtShareSession;
}
set
...{
this._HtShareSession = value;
}
}
public SessionEntity(string guid)
...{
this.AUTH_GUID = guid;
}
}//
}//
3、创建一个序列化、反序列化操作的类:ShareSessionFormatter
代码如下:
using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.IO;
namespace ShareSession
...{
/**//* *****************************
*
* Author : Xiaojun Liu
* Create Date : 2007.11.09
* Description :
*
* *****************************
*/
/**//// <summary>
/// 格式化操作器
/// </summary>
public class ShareSessionFormatter
...{
private static string _ShareSessionPath = System.Configuration.ConfigurationManager.AppSettings["ShareSessionPath"].ToString();
/**//// <summary>
/// 序列化格式器类型
/// </summary>
public enum FormatterType
...{
Binary,
Soap
};
/**//// <summary>
/// 执行序列化
/// </summary>
/// <param name="formatterType">类型</param>
public static void Serialize(FormatterType formatterType)
...{
if (HttpContext.Current.Session["AUTH_GUID"] == null)
...{
HttpContext.Current.Session["AUTH_GUID"] = Guid.NewGuid().ToString();
}
Hashtable ht = new Hashtable();
//遍历Session
foreach (string key in HttpContext.Current.Session.Contents.Keys)
...{
ht.Add(key, HttpContext.Current.Session[key]);
}
/**/////执行序列化
switch (formatterType)
...{
case FormatterType.Binary:
BinarySerialize(ht);
break;
case FormatterType.Soap:
SoapSerialize(ht);
break;
default:
break;
}
}
/**//// <summary>
/// 按照Binary格式序列化
/// </summary>
/// <param name="ht"></param>
private static void BinarySerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;
// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
//执行序列化
formatter.Serialize(stream, obj);
stream.Close();
// 将对象置空
obj = null;
}
/**//// <summary>
/// 按照Soap格式序列化
/// </summary>
/// <param name="ht"></param>
private static void SoapSerialize(Hashtable ht)
...{
string guid = HttpContext.Current.Session["AUTH_GUID"].ToString();
SessionEntity obj = new SessionEntity(guid);
obj.HtShareSession = ht;
// 创建一个文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
//执行序列化
formatter.Serialize(stream, obj);
stream.Close();
// 将对象置空
obj = null;
}
/**//// <summary>
/// 反序列化
/// </summary>
/// <param name="formatterType">传送端的Session["AUTH_GUID"]</param>
/// <param name="guid"></param>
public static void Deserialize(FormatterType formatterType, string guid)
...{
switch (formatterType)
...{
case FormatterType.Binary:
BinaryDeserialize(guid);
break;
case FormatterType.Soap:
SoapDeserialize(guid);
break;
default:
break;
}
}
/**//// <summary>
/// 以Binary方式反序列化
/// </summary>
private static void BinaryDeserialize(string guid)
...{
SessionEntity obj = new SessionEntity(guid);
// 打开文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
obj = (SessionEntity)formatter.Deserialize(stream);
stream.Close();
Hashtable ht = obj.HtShareSession;
obj = null;
//遍历ht,生成Session
CreateSession(ht);
}
/**//// <summary>
/// 以Soap方式反序列化
/// </summary>
private static void SoapDeserialize(string guid)
...{
SessionEntity obj = new SessionEntity(guid);
// 打开文件guid.xml
Stream stream = File.Open(_ShareSessionPath + guid + ".xml", FileMode.Open);
SoapFormatter formatter = new SoapFormatter();
obj = (SessionEntity)formatter.Deserialize(stream);
stream.Close();
Hashtable ht = obj.HtShareSession;
obj = null;
//遍历ht,生成Session
CreateSession(ht);
}
/**//// <summary>
/// 遍历Hashtable,同时创建Session
/// </summary>
/// <param name="ht"></param>
private static void CreateSession(Hashtable ht)
...{
foreach (DictionaryEntry de in ht)
...{
HttpContext.Current.Session[de.Key.ToString()] = de.Value;
}
}
}//
}//
4、编译项目,生成ShareSession.dll
5、把ShareSession.dll引入Web Server 1上的应用程序中,同时在Web.config文件中增加配置字节
代码如下:
<!-- Session信息序列化后保存路径 -->
<add key="ShareSessionPath" value="C:\ShareSession\"/>
6、在Web Server 1上的应用程序中新建一个页面,功能是初始化Session,然后把Session信息序列化,代码如下:
前台代码:
<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="ShareSession.aspx.cs" Inherits="Front_Test_ShareSession" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Session共享</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lHttp_Cookie" runat="server" Text=""></asp:Label>
<a href="http://localhost:5000/Front/Test/ShareSession.aspx?AUTH_GUID=<%=AUTH_GUID %>">Go Other Web Site(port:5000)</a>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Front_Test_ShareSession : System.Web.UI.Page
...{
public string AUTH_GUID = "";
protected void Page_Load(object sender, EventArgs e)
...{
Session.Clear();
Session.Abandon();
Session.Add("USER_ID", "2002");
Session.Add("USER_NAME", "Xiaojun Liu");
ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
AUTH_GUID = Session["AUTH_GUID"].ToString();
}
}
7、在Web Server 2上进行第5步操作
8、在Web Server 2上的应用程序中新建一个页面,功能是反序列化,还原Session,同时读取Session信息进行测试,代码如下:
--------------------编程问答--------------------
前台代码:
<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="ShareSession.aspx.cs" Inherits="Front_Test_ShareSession" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Session共享</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lHttp_Cookie" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Front_Test_ShareSession : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
string guid = Request.Params["AUTH_GUID"].ToString();
ShareSession.ShareSessionFormatter.Deserialize(ShareSession.ShareSessionFormatter.FormatterType.Soap,guid);
Response.Write("USER_ID = "+Session["USER_ID"].ToString()+"<br />");
Response.Write("USER_NAME = "+Session["USER_NAME"].ToString()+"<br />");
Response.Write("AUTH_GUID = " + Session["AUTH_GUID"].ToString() + "<br />");
}
}
此例子只是提供了一种解决思路,在应用过程中应根据项目不同进行调整及详细设计 --------------------编程问答-------------------- 大家快想想办法啊 --------------------编程问答-------------------- 站点a:session插入数据库
站点B:数据库中读取session
--------------------编程问答-------------------- 不懂HTML原理
不能跨站
点跨站的可以把登录信息用URL参数加密方式传过来,同时把登录信息放到session里就行了 --------------------编程问答-------------------- 大家再想想有没有好的办法可以实现的..... --------------------编程问答-------------------- 所有的页面都继承一个基类,该基类写一个虚page_load函数,获取传过来的值,给session赋值。
这个基类继承System.Web.UI.Page --------------------编程问答-------------------- Ranen2010 说的方法可行。session是不可以跨站的,必须自己代码处理。 --------------------编程问答-------------------- 还是将值存入数据库,另一个网站根据条件再去查出来了啊
不了解你这种需求的发生环境,感觉有种SSO的味道 --------------------编程问答-------------------- --------------------编程问答-------------------- IE认为夸站Seesion是不安全的 --------------------编程问答-------------------- 第一个站吧session传递过去,第二个站再新建session
补充:.NET技术 , ASP.NET