【发布时间】: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