私は人々が同様の問題を抱えているが、実用的な解決策を見つけられなかった多くの投稿を読みました。私はMVC4サイトを持っていますが、ページをキャッシュしたいので、Webサイト全体からキャッシュを削除したくありません。ユーザーがログオフボタンをクリックすると、正常にログオフしてログインページにリダイレクトされますが、ユーザーが戻るボタンをクリックすると、以前に表示された「制限付きページ」が表示され、ログインした場合にのみ表示されます。これは、ブラウザがページクライアント側をキャッシュしているためです。私はいくつかの解決策を試しましたが、前述のように、どれも機能しません。現在、私のログオフには次のコードがあります。
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
Session.Abandon();
Session.Clear();
// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);
// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);
// Invalidate the Cache on the Client Side
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");
// send an expired cookie back to the browser
var ticketExpiration = DateTime.Now.AddDays(-7);
var ticket = new FormsAuthenticationTicket(
1,
// replace with username if this is the wrong cookie name
FormsAuthentication.FormsCookieName,
DateTime.Now,
ticketExpiration,
false,
String.Empty);
var cookie = new System.Web.HttpCookie("user")
{
Expires = ticketExpiration,
Value = FormsAuthentication.Encrypt(ticket),
HttpOnly = true
};
Response.Cookies.Add(cookie);
return RedirectToAction("Login", "Account");
}