programing

로그아웃 시 세션을 지우는 방법

yellowcard 2023. 7. 9. 10:59
반응형

로그아웃 시 세션을 지우는 방법

사용자가 로그아웃을 클릭하면 로그인 페이지로 사용자를 리디렉션하지만 사용자가 다시 로그인할 때 모든 데이터가 지속되었기 때문에 응용 프로그램이나 세션이 지워지지 않는 것 같습니다.

현재 로그인 페이지에는 로그인 컨트롤이 있으며 페이지 뒤에 있는 코드는 로그인 인증에만 연결되어 있습니다.

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()End(종료)가 실행되지 않으며 클라이언트의 추가 요청으로 인해 Session Start 이벤트가 발생하지 않습니다.

Session.Abandon()세션을 파괴합니다.Session_OnEnd이벤트가 트리거됩니다.

Session.Clear()개체에서 모든 값(내용)을 제거합니다.session with the same key아직도alive.

그래서 만약에.Session.Abandon()특정 세션을 잃게 되면 사용자는new session key예를 들어 사용자가 사용할 수 있는 경우logs out.

사용하다Session.Clear()사용자가 동일한 세션에 남아 있기를 원하는 경우(예: 사용자가 다시 로그인하지 않기를 원하는 경우) 모든 세션 관련 데이터를 재설정합니다.

.NET 코어의 경우 세션을 지우는 방법이 조금 다릅니다.거기에는 없다Abandon()기능.

ASP.NET Core 1.0 이상

//Removes all entries from the current session, if any. The session cookie is not removed.
HttpContext.Session.Clear()

여기에서 api 참조 참조 참조

.NET Framework 4.5 이상

//Removes all keys and values from the session-state collection.
HttpContext.Current.Session.Clear(); 

//Cancels the current session.
HttpContext.Current.Session.Abandon();

여기에서 api 참조 참조 참조

<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>  

session.session은(는) 세션을 제거하지 않습니다.브라우저의 쿠키 ID입니다.따라서 이 이후의 모든 새 요청은 동일한 세션 ID를 사용합니다.따라서 응답을 사용합니다.쿠키.추가(새 HttpCookie("ASP").NET_SessionId", ""); session.abandon() 뒤에 있습니다.

.Net 코어의 경우

[HttpPost]
    public IActionResult Logout()
    {
        try
        {
            CookieOptions option = new CookieOptions();
            if (Request.Cookies[AllSessionKeys.AuthenticationToken] != null)
            {
                option.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Append(AllSessionKeys.AuthenticationToken, "", option);
            }

            HttpContext.Session.Clear();
            return RedirectToAction("Login", "Portal");
        }
        catch (Exception)
        {
            throw;
        }
    }

세션.지우기();

프로젝트의 파일 Global.asax.cs 로 이동하여 다음 코드를 추가합니다.

    protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddHours(-1));
        Response.Cache.SetNoStore();
    }

저한테는 효과가 있었어요...!참조 링크 로그아웃 MVC 4의 세션 지우기

언급URL : https://stackoverflow.com/questions/345157/how-to-clear-out-session-on-log-out

반응형