基本思路:
1 Session源网站设置Session数据同时,把SessionID和Session数据一起插入一个数据库中,再把SessionID作为查询字符串传递到Session获取网站.
2 Session获取网站从数据库中按SessionID查询获取Session数据并赋值到本网站的Session中.
示例:
Session源网站部分:
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
this.TextBox1.Text = Session.SessionID;
Session["Name"] = this.TextBox2.Text;
Session["Role"] = this.TextBox3.Text;
OleDbConnection conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\webTest.mdb;Persist Security Info=False" );
string strInsertSql = "insert into SessionData "
+ " ( SessionID, SessionName, SessionRole ) "
+ " values "
+ "( '" + Session.SessionID + "', '" + Session["Name"] + "', '" + Session["Role"] + "' )";
conn.Open();
OleDbCommand cmd = new OleDbCommand( strInsertSql, conn );
cmd.ExecuteNonQuery();
conn.Close();
this.TextBox1.Text = "Session保存成功";
string strJumpUrl = "http://localhost/SessionReadFromOtherSite/ReadOtherSession.aspx?SessionId=" + Session.SessionID;
Response.Write("<script>window.open('" + strJumpUrl + "');</script>");
}
catch( System.Exception ex )
{
this.TextBox1.Text = ex.Message;
}
}
{
try
{
this.TextBox1.Text = Session.SessionID;
Session["Name"] = this.TextBox2.Text;
Session["Role"] = this.TextBox3.Text;
OleDbConnection conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\webTest.mdb;Persist Security Info=False" );
string strInsertSql = "insert into SessionData "
+ " ( SessionID, SessionName, SessionRole ) "
+ " values "
+ "( '" + Session.SessionID + "', '" + Session["Name"] + "', '" + Session["Role"] + "' )";
conn.Open();
OleDbCommand cmd = new OleDbCommand( strInsertSql, conn );
cmd.ExecuteNonQuery();
conn.Close();
this.TextBox1.Text = "Session保存成功";
string strJumpUrl = "http://localhost/SessionReadFromOtherSite/ReadOtherSession.aspx?SessionId=" + Session.SessionID;
Response.Write("<script>window.open('" + strJumpUrl + "');</script>");
}
catch( System.Exception ex )
{
this.TextBox1.Text = ex.Message;
}
}
Session获取网站部分:
private void Page_Load(object sender, System.EventArgs e)
{
try
{
if ( Request.QueryString["SessionID"] != null )
{
OleDbConnection conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\webTest.mdb;Persist Security Info=False" );
string strSql = "select "
+ " SessionID, SessionName, SessionRole "
+ " from SessionData "
+ " where SessionID = '" + Request.QueryString["SessionID"].ToString() + "'";
OleDbDataAdapter da = new OleDbDataAdapter( strSql, conn );
DataSet ds = new DataSet();
da.Fill( ds );
Session["Name"] = ds.Tables[0].Rows[0]["SessionName"].ToString();
Session["Role"] = ds.Tables[0].Rows[0]["SessionRole"].ToString();
this.TextBox1.Text = ds.Tables[0].Rows[0]["SessionID"].ToString();
this.TextBox2.Text = Session["Name"].ToString();
this.TextBox3.Text = Session["Role"].ToString();
}
}
catch( System.Exception ex )
{
this.TextBox1.Text = ex.Message;
}
}
{
try
{
if ( Request.QueryString["SessionID"] != null )
{
OleDbConnection conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\webTest.mdb;Persist Security Info=False" );
string strSql = "select "
+ " SessionID, SessionName, SessionRole "
+ " from SessionData "
+ " where SessionID = '" + Request.QueryString["SessionID"].ToString() + "'";
OleDbDataAdapter da = new OleDbDataAdapter( strSql, conn );
DataSet ds = new DataSet();
da.Fill( ds );
Session["Name"] = ds.Tables[0].Rows[0]["SessionName"].ToString();
Session["Role"] = ds.Tables[0].Rows[0]["SessionRole"].ToString();
this.TextBox1.Text = ds.Tables[0].Rows[0]["SessionID"].ToString();
this.TextBox2.Text = Session["Name"].ToString();
this.TextBox3.Text = Session["Role"].ToString();
}
}
catch( System.Exception ex )
{
this.TextBox1.Text = ex.Message;
}
}