iT邦幫忙

1

ASP.NET 在Iframe Session 消失問題

  • 分享至 

  • twitterImage

如標題,已爬文大概網路上都如黑暗大大說的,實測結果無效。
猜測可能是狀況不一樣所以無效......
https://blog.darkthread.net/blog/p3p-header-and-iframe-session/

今天遇到一個小問題...想請各位武林高手幫忙。
使用技術Asp.net webform & C#。
開發環境VS2017,開啟新的空白webform專案 debug測試下方問題。
狀況

  1. F5偵錯=>轉址=>Session存在
  2. 將debug網址轉到其他地方 => tw.yahoo.com
  3. 將網址轉回==>debug網址 ==> Session 消失

描述: session在Default.aspx還存在,轉址之後再top.aspx就消失了..,但是按下F5 session又出現了,懇請各路大大解惑

Default.aspx

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Response.Headers.Add("P3P", "CP=\"NOI ADM DEV COM NAV OUR\"");

                Session["AA"] = "alonso";
                Response.Redirect("Main.htm");

                
            }
        }

    }

"Main.htm"

<!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>
    <title>入口網站</title>
</head>
<!--
<frameset rows="54,*,27" >
 -->
<frameset rows="55,*">
    <frame src="top.aspx" frameborder="0" marginheight="0" noresize="True" scrolling="no">
        <frameset cols="185,*" border="0">
            <frame src="menu.aspx" scrolling="yes" marginwidth="0" marginheight="10" frameborder="0">
            <frame  src="Content.aspx" name="Content" frameborder="0">
        </frameset>
        <!--
        <frame src="bottom.aspx" noresize="True" marginheight="0" frameborder="0" >
        -->


</frameset>
<body>
</body>
</html>

top.aspx

  public partial class top : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Headers.Add("P3P", "CP=\"NOI ADM DEV COM NAV OUR\"");

            Response.Write(Session["AA"]);  
        }
    }

=====================
更新1
自己測試發現第一次 get 有setcookie
去了別的網頁又回來,第二次,沒有set cookie.... ==> 在GET一次就正常了
更新2
目前發現,在第二次GET的地方,session id沒有在 top.aspx的http header出現...
必須再點一次get才會出現...
更新3
在webconfig的sessionState設定為cookieSameSite="None",解決問題。
But這樣似乎太危險.....還是要想辦法...

看更多先前的討論...收起先前的討論...
測了一下沒有發現這種情形
不太明白 session 消失的意思?

我的流程
Default.aspx -> Main.htm -> yahoo -> 按上一頁返回
網頁上 alonso 都存在著
回fysh711426 大大
Default.aspx -> Main.htm -> yahoo -> 再打一次Default.aspx 用get的方式,試試看
問題已解
參考解決來源
https://www.lagou.com/lgeduarticle/113305.html

https://forums.asp.net/t/2161961.aspx?Problem+with+Session+in+iFrame+after+recent+windows+update
第一次看到 Cookie SameSite
原來現在多了這個安全策略
不過我還是沒有測出來,哈哈哈
入口網站和其他 webform 需要在不同網域嗎?
需要過程是, http://localhost/Default.aspx => http://tw.yahoo=> http://localhost/Default.aspx
好,我再試試 ~
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
player
iT邦大師 1 級 ‧ 2020-06-02 20:30:18

你有把ASP.NET的Session
改到用ASP.NET狀態服務嗎?

修改 web.config 檔

<configuration>
  <system.web>
    <sessionState mode="StateServer"
      stateConnectionString="tcpip=localhost:42424"
      cookieless="false"
      timeout="30"/>
  </system.web>
</configuration>

1.「開始」→ 「設定」→「控制台」 → 「系統管理工具」 → 「服務」
2.「ASP.NET 狀態服務」 或 「ASP.NET State Service」
3. 「啟動類型」 改為「自動」後,「套用」再直按「啟動」即可。

回大大有改到asp.net State,並且有啟動服務

我要發表回答

立即登入回答