Asp.net跨網站傳遞Session

來源:互聯網
上載者:User

基本思路:
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;
            }
        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.