会话更改其ID

时间:2018-10-26 17:02:01

标签: c# asp.net session-cookies

我正在更改会话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("/");
            }

// ..

        }

1 个答案:

答案 0 :(得分:0)

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

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