2012-03-02 100 views
1

我必須更改ASP.NET 4.0應用程序(SessionID通過Cookie)的SessionID,使其在具有相同Session的兩個域(http://web1.local和http://web2.local)上運行。這將保持項目在2個域中同步。ASP.NET更改SessionID

因此,Page將具有帶「setkey.aspx/ashx」的iframe,它必須設置新的SessionID。

<iframe src="http://web1.local/SetKey.aspx?sid=<%=Session.SessionID%>" width="250" height="100"></iframe> 
<iframe src="http://web2.local/SetKey.aspx?sid=<%=Session.SessionID%>" width="250" height="100"></iframe> 

在Session.Start中Session已經被填充,所以SessionID通過來自同一個Broeser的幾個請求保持靜態。

我發現了幾個職位,主要使用SessionIDManager.SaveSessionID,但我可以得到他們沒有工作。

我發現了這段代碼,但是我要在哪裏調用它? (global.asax/default.aspx/setjey.aspx/ashx?)

Dim Redirected As Boolean = False 
Dim Added As Boolean = False 

Dim SID = Request.QueryString("sid") 
If Not String.IsNullOrEmpty(SID) AndAlso SID.Length = 24 Then 
    Dim SM As New SessionIDManager 
    SM.InitializeRequest(HttpContext.Current, False, True) 
    SM.RemoveSessionID(HttpContext.Current) 
    SM.SaveSessionID(HttpContext.Current, SID, Redirected, Added) 
End If 

有沒有人得到此代碼來正確運行?

+0

會使用SQL狀態服務器是一個選項嗎? http://support.microsoft.com/kb/317604 – 2012-03-02 18:33:37

+0

嗨,不,因爲SQL Server會花費太多的性能。 – Christoph 2012-03-05 13:00:47

回答

0

我試着簡單地在Global.asax的Application_EndRequest處理程序中設置cookie,它可以工作。這裏用C#,但你明白了......

void Application_EndRequest(object sender, EventArgs e) 
{ 
    var ctx = HttpContext.Current; 
    ctx.Response.Cookies.Remove("ASP.NET_SessionId"); 
    ctx.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ctx.Request.QueryString("sid"))); 
} 
+0

EndRequest是我沒有嘗試的唯一方法:-)謝謝! – Christoph 2012-03-02 16:44:39

+0

這是在Chrome瀏覽器,火狐瀏覽器,Safari瀏覽器和Opera的工作,但不是在IE9 :-( – Christoph 2012-03-02 16:46:37

+0

嗯...我猜是IE瀏覽器使它只讀。嘗試改變cookie名稱的東西(如SessionID)通過添加cookieName =「SessoinID」到你的web.config文件中的標籤中,這應該會修復IE,不要忘記相應地更改代碼 – Diego 2012-03-02 21:35:36