ログアウト時にセッションをクリアする方法
-
19-08-2019 - |
質問
ユーザーがログアウトするときにユーザーをログインページにリダイレクトしますが、ユーザーが再びログインしたときにすべてのデータが保持されるため、アプリケーションやセッションがクリアされるとは思わない。
現在、ログインページにはログインコントロールがあり、ページ上のコードビハインドはログイン認証のみに関連付けられています。
ASP.NET Webサイトへのログインおよびログアウトの処理に関する優れたチュートリアルまたは記事に誰かを誘導できますか?
解決
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
です。
したがって、new session key
を使用すると、その特定のセッションが失われ、ユーザーはlogs out
を取得します。たとえば、ユーザー<=>の場合に使用できます。
<=>を使用して、ユーザーを同じセッションに残し(たとえば、ユーザーに再ログインさせたくない場合)、セッション固有のデータをすべてリセットします。
プロジェクトの 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コアでは少し異なります。 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 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();
Session.Clear();
session.abandon()は、ブラウザからsessionID Cookieを削除しません。したがって、これ以降の新しいリクエストは同じセッションIDを取得します。 したがって、Response.Cookies.Add(new HttpCookie(<!> quot; ASP.NET_SessionId <!> quot ;, <!> quot; <!> quot;));を使用します。 session.abandon()の後。