로그 아웃에서 세션을 지우는 방법
-
19-08-2019 - |
문제
사용자가 로그 아웃을 클릭하면 사용자를 로그인 페이지로 리디렉션하지만 사용자가 로그인 할 때 모든 데이터가 지속되므로 응용 프로그램이나 세션이 지워지지 않는다고 생각합니다.
현재 로그인 페이지에는 로그인 컨트롤이 있으며 페이지 뒤의 코드는 로그인 인증 만 연결됩니다.
누군가가 ASP.NET 웹 사이트에서 로그인을 처리하고 나가는 것에 대한 좋은 튜토리얼이나 기사로 나를 안내 할 수 있습니까?
해결책
Session.Abandon()
http://msdn.microsoft.com/en-us/library/ms524310.aspx
다음은 다음에 더 자세한 내용입니다 HttpSessionState
물체:
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate_members.aspx
다른 팁
나는 다음을 사용하여 세션을 클리어하고 클리어합니다 aspnet_sessionID
:
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
내가 선호하는 Session.Abandon()
Session.Clear()
화재가 발생하지 않으며 클라이언트의 추가 요청은 세션 시작 이벤트를 제기하지 않습니다.
Session.Abandon()
세션을 파괴합니다 Session_OnEnd
이벤트가 트리거됩니다.
Session.Clear()
객체에서 모든 값 (내용) 만 제거합니다. 그만큼 session with the same key
아직 alive
.
따라서 사용하는 경우 Session.Abandon()
, 당신은 그 특정 세션을 잃고 사용자는 new session key
. 예를 들어 사용자가 사용할 수 있습니다 logs out
.
사용 Session.Clear()
, 사용자가 동일한 세션에 남아있는 경우 (예 : 재발하는 것을 원하지 않는 경우) 모든 세션 특정 데이터를 재설정하십시오.
파일로 이동하십시오 Global.asax.cs 프로젝트에서 다음 코드를 추가하십시오.
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddHours(-1));
Response.Cache.SetNoStore();
}
그것은 나를 위해 일했다 ..! 참조 링크 로그 아웃 MVC에 대한 클리어 세션 4
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
Session["FavoriteSoftware"] = "Adobe ColdFusion";
Label1.Text = "Session read...<br />";
Label1.Text += "Favorite Software : " + Session["FavoriteSoftware"];
Label1.Text += "<br />SessionID : " + Session.SessionID;
Label1.Text += "<br> Now clear the current session data.";
Session.Clear();
Label1.Text += "<br /><br />SessionID : " + Session.SessionID;
Label1.Text += "<br />Favorite Software[after clear]: " + Session["FavoriteSoftware"];
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net session Clear example: how to clear the current session data (remove all the session items)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net session example: Session Clear</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="DarkMagenta"
>
</asp:Label>
</div>
</form>
</body>
</html>
세션을 지우는 방법은 .NET Core와 약간 다릅니다. 아니요 Abandon()
기능.
ASP.NET CORE 1.0 이상
//Removes all entries from the current session, if any. The session cookie is not removed.
HttpContext.Session.Clear()
.NET 프레임 워크 4.5 이상
//Removes all keys and values from the session-state collection.
HttpContext.Current.Session.Clear();
//Cancels the current session.
HttpContext.Current.Session.Abandon();
session.clear ();
Session.abandon ()은 브라우저에서 SessionID 쿠키를 제거하지 않습니다. 따라서 그 후 새 요청은 동일한 세션 ID를 취합니다. 따라서, response.cookies.add를 사용합니다 (new httpcookie ( "asp.net_sessionid", ""); 세션 후 .Abandon ().