gpt4 book ai didi

c# - session 在更改其 ID 时丢失键和值

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:27 30 4
gpt4 key购买 nike

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

// ..

}

最佳答案

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

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

关于c# - session 在更改其 ID 时丢失键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53013347/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com