【问题标题】:Session ID does not change on logout会话 ID 在注销时不会更改
【发布时间】:2015-10-12 14:31:10
【问题描述】:

我已经搜索了几乎所有内容,但看起来太混乱了。我有一个登录控件,如下所示:

<asp:Login ID="Login1" runat="server" Width="300px" onauthenticate="LoginControl_Authenticate" onloggedout="LoginStatus1_LoggedOut">

它登录用户并使用 cookie 进行身份验证。我还添加了额外的代码以确保整个会话在注销按钮时被销毁,如下所示;

void LoginStatus1_LoggedOut(Object sender, System.EventArgs e)
{
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
    Response.Redirect("~/Login.aspx");
}

即使在注销后会话 ID 仍然保持不变(例如,如果我注销它不会将我引导到我想要的登录页面..所以看起来它甚至没有执行上述事件,因为我尝试放置一个断点。当我重新登录时,它仍然带有相同的会话 ID。我在这里做错了什么 ? 另一件事是我将会话 ID 显示为:

 Session["SessionId"] = Session.SessionID;

我正在创建一个像

这样的新会话
 HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

这两个会话是不同的还是相同的?

我有哪些选项可以在注销时清除所有会话变量。

注意:我在 Global.asax 文件中也有这个,但没有帮助

 void Session_End(object sender, EventArgs e) 
{
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
}

【问题讨论】:

  • 只需使用FormsAuthentication.SignOut(); Session.Abandon(); 而不是HttpContext.Current.Session.Clear(); HttpContext.Current.Session.Abandon();
  • @SankarRaj OP 已经在 LoggedOut 事件中。这意味着 FormsAuthentication.SignOut() 已经被调用了。
  • @SankarRaj - 这是否会永远破坏会话,我的意思是即使单击后退按钮,用户也应该保持注销状态。
  • @SankarRaj - 我使用 FormsAuthentication.SignOut();在 Page_Load 上
  • @RelatedRhymes 您不需要显式调用 FormsAuthentication.SignOut()。如果您到达 LoginStatus1_LoggedOut 事件,则 FormsAuthentication.SignOut() 已被调用。仅供参考,FormsAuthentication 与清除会话状态无关。

标签: asp.net session cookies login


【解决方案1】:

首先,您不想直接操作 ASP.Net Session cookie ASP.NET_SessionId

无论您是否登录,会话状态都将可用,因此您无需担心Session.SessionID

换句话说,您只关心您添加到 Session 的值对。

例如,清除 Session 状态后,以下代码中的 myValue 应为 null。

protected void LoginStatus1_LoggedOut(Object sender, System.EventArgs e)
{
    Session.Clear();
    Session.RemoveAll();
    Session.Abandon();
    var myValue = Session["MyValue"];
    Response.Redirect("~/Login.aspx");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2018-08-17
    • 1970-01-01
    相关资源
    最近更新 更多