【问题标题】:asp.net can you mix cookieless with cookie session stored session data?asp.net 可以将 cookieless 与 cookie 会话存储的会话数据混合使用吗?
【发布时间】:2011-07-18 21:36:35
【问题描述】:

是否可以在 cookie 会话中使用混合无 cookie 会话?

我有一个应用程序可以捕获用户详细信息,然后将付款重定向到 ssl 页面。我想知道这是否可能?

http://www.mydomain.com/confirm.aspx

重定向到

https://www.mydomain.com/(S(za1tw2l2k02jer4fiskzlovd))/payment.aspx

注意:后一个 url 中的 session Id。

所以本质上,我们对大多数应用程序使用标准 cookie 会话,但是当我们转移到 ssl 页面时,我们将 SessionId 传递给 https url 以获取会话。我已经在本地尝试过,但它会启动一个新会话。

我错过了什么技巧吗?

谢谢

【问题讨论】:

    标签: asp.net session-cookies cookieless


    【解决方案1】:

    我找到了一个似乎可行的解决方案

    在 http 和 https 之间传输时,我有以下内容:

    如您所见,我将会话 ID 手动传递给 https 页面。

    protected void btnPurchase_Click(object sender, EventArgs e)
    {
            // Confirm puchase code **
    
            string sslPaymentPath = string.Format("https://{0}/payment.aspx?sid={1}", Request.Url.DnsSafeHost, Session.SessionID);
    
            Response.Redirect(sslPaymentPath);
    
    }
    

    到达 ssl 页面后,asp.net 将请求视为一个新会话,因此我使用 global.asax 中的 Start_Session 方法放弃新创建的会话并添加一个新的会话 cookie,其中包含从请求参数。因为此时填充会话 keyValue 对的 AquireSessionState 已经运行,所以我需要将页面重定向回自身以重新填充这些值。

    它似乎工作得很好:)

        void Session_Start(object sender, EventArgs e)
        {
            bool isPaymentPage = (Request.Path.ToLower().IndexOf("payment.aspx") != -1);
    
            // Code to load session over ssl. When changing between two sessions
            if (isPaymentPage && Request.QueryString["sid"] != null && Request.IsSecureConnection)
            {
                string passedSessionId = Request.QueryString["sid"];
                Session.Abandon();
                Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", passedSessionId));
                Response.Redirect(Request.Url.LocalPath, true);
            }
        }
    

    另外关于有人在浏览 ssl purchase.aspx 页面时单击外部链接,我在 global.asax 中编写了以下内容,以将流量重定向回标准的非 ssl 页面(如果它不是付款页面)。

    void Application_BeginRequest(object sender, EventArgs e)
        {
            bool isPaymentPage = (Request.Path.ToLower().IndexOf("payment.aspx") != -1);
    
            // In the case someone has navigated away from the payment page redirect them back to the none secure protocol.
            if (!isPaymentPage && Request.IsSecureConnection)
            {
                bool isAxdResource = (Request.Path.ToLower().IndexOf(".axd") != -1);
    
                if (!isAxdResource)
                {
                    string url = Request.Url.AbsoluteUri.ToLower().Replace("https://", "http://");
                    Response.Redirect(url,true);
                }
            }
        }
    

    希望有人觉得这很有用,我被困了一段时间试图想出一个好的解决方案。

    我的灵感来自this url

    【讨论】:

    • 我还没有进行任何实际测试,但这不是让“劫持”会话(检查查询字符串)变得微不足道吗?此外,有关 https 部分位于不同域中的原始问题(需要解决方法)吗?
    • 原来的问题是关于在 ssl 和 http 之间切换会话,http 和 https 被服务器分开处理,所以当切换到 ssl 时浏览器必须启动一个新会话。这只是重新捕获现有用户会话的一种解决方法。我想您可以劫持用户会话,但您需要获取用户会话 ID。您必须提高风险/便利性。我使用它的网站不需要用户登录或公开任何敏感信息,因此安全考虑很少。
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2015-07-23
    • 2010-12-11
    • 2014-10-06
    • 2018-10-06
    • 1970-01-01
    • 2013-02-22
    • 2020-11-10
    相关资源
    最近更新 更多