【问题标题】:Session loses keys and values when changing its ID会话在更改其 ID 时丢失键和值
【发布时间】:2019-03-31 11:41:18
【问题描述】:

我正在更改会话 ID,但是当它重定向到 Default.aspx 页面时,它会丢失我分配给它的所有键!

这个奇怪的,有什么线索或帮助吗?

即使我在评论这部分:

Session.Clear();
            Session.Abandon();
            Session.RemoveAll();
            if (Request.Cookies["ASP.NET_SessionId"] != null)
            {
                Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
                Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
            }

它失去了一切!

这是我的代码:

protected void btnDebugLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string email = "test@123.com";
                string pw = "password";
                string ip = Request.UserHostAddress.ToString();
                string browseragent = Request.UserAgent.ToString();
                ConsoleUser loginUser = new ConsoleUser();               

                AbandonSession();//Delete any existing sessions
                var newSessionId = CreateSessionId(HttpContext.Current); //Create a new SessionId
                SetSessionId(HttpContext.Current, newSessionId);

                loginUser = SecureLogin.Login(email, pw, ip, browseragent, referrer, LangCode, Session.SessionID.ToString(), null);

                if (loginUser == null)
                {
                    lblMsg.Visible = true;
                }
                else
                {
                    Session["CurrentUser"] = loginUser;
                    Session["CurrentLoginID"] = loginUser.CurrentLoginId; // Used for tracking User Activity in KeepSessionAlive
                    Response.Redirect("/qConsole/Default.aspx",false);
                }
            }
            catch(Exception ex)
            {

            }
        }


 protected void AbandonSession()
        {
            Session.Clear();
            Session.Abandon();
            Session.RemoveAll();
            if (Request.Cookies["ASP.NET_SessionId"] != null)
            {
                Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
                Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
            }
            if (Request.Cookies["__AntiXsrfToken"] != null)
            {
                Response.Cookies["__AntiXsrfToken"].Value = string.Empty;
                Response.Cookies["__AntiXsrfToken"].Expires = DateTime.Now.AddMonths(-20);
            }
        }

        private static string CreateSessionId(HttpContext httpContext)
        {
            var manager = new SessionIDManager();

            string newSessionId = manager.CreateSessionID(httpContext);

            return newSessionId;
        }

        public static void SetSessionId(HttpContext httpContext, string newSessionId)
        {
            var manager = new SessionIDManager();

            bool redirected;
            bool cookieAdded;

            manager.SaveSessionID(httpContext, newSessionId, out redirected, out cookieAdded);

        }

验证部分在加载 Default.apsx 页面之前在母版页中完成,这里:

 protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
            {
                Response.Redirect("/");
            }

// ..

        }

【问题讨论】:

  • 这实际上听起来像它完全应该如何工作。会话变量存储在字典中,key = SessionID。您只能使用原始 ID 检索它们。
  • 但在使用 SessionIDManager 更改 ID 后,我将 CurrenUser 和 CurrentLoginID 对象分配给 Session!对不对?
  • 您将无法在同一个请求中删除旧会话并创建新会话 - ASP.NET 将发出两个具有相同名称的 cookie 标头,一个已过期,一个具有新 ID .不知道这将如何发挥作用。你就不能set the ID you want to begin with吗?
  • 我的意图只是在登录后更改ID,这可能与我实现的方式不同吗?
  • 为什么还要更改会话 ID?

标签: c# asp.net session-cookies


【解决方案1】:

这是告诉客户端使用新会话 ID 的预期结果。值仍在旧会话中,但旧会话和新会话之间没有任何联系。 Session 在请求周期的早期附加到请求,并且在处理期间更改 cookie 值不会影响在下一个请求到来之前附加到用户请求的会话。您要做的是清除 cookie当您第一次呈现页面时,而不是当他们单击按钮时。我在回答 https://stackoverflow.com/a/45383679/10558 中提到了会话管理的其他一些微妙之处。

您已标记此问题 csrf,但您的解决方案对该攻击没有任何作用。重置会话 ID 阻止的是会话固定。

【讨论】:

  • 我的意图只是在登录后更改ID,这可能与我实现的方式不同吗?
  • 是的,但是当您更改 ID 时,您会丢失之前的所有状态,包括他们已经登录。如果您想使用 ASP.Net 会话框架,您需要在请求之前更改 id他们在哪里登录。您可以在发送登录页面时执行此操作,只需确保不要在会话中放置任何内容。
猜你喜欢
  • 2013-08-03
  • 1970-01-01
  • 2019-08-13
  • 2017-12-03
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2023-03-28
相关资源
最近更新 更多