分享

Asp.net跨网站传递Session

 趋明 2012-02-16


时间:2011-01-09 09:34来源:未知 作者:admin 点击:44次
-
-
基本思路:
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;
            }       
        }
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;
            }
        }
 

本篇文章来源于 www. 原文链接:http://www./html/aspnet/objects/2011/0109/3338.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多