25

こんにちは、初めて MVC でソリューションを開発しているので、大きな問題に直面しています。アプリケーション (mvc razor Web アプリケーション) からログアウトすると、ログイン ページが表示されますが、ブラウザの [戻る] ボタンを押すと、最後の画面が表示されます。これは必要ありません。[戻る] ボタンを押しても同じログイン ページが表示されます。ここにログアウト用の私のコードがあります

public ActionResult Logout()
    {
        Session.Clear();
        Session.Abandon();
        Session.RemoveAll();

        FormsAuthentication.SignOut();


        this.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        this.Response.Cache.SetNoStore();          

        return RedirectToAction("Login");
    }
4

6 に答える 6

65

少し前にこの問題がありました。アプリケーション全体のキャッシュを無効にすると問題が解決しました。これらの行をGlobal.asax.csファイルに追加するだけです

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

お役に立てれば。

于 2013-10-11T10:23:49.247 に答える
11

META最後にアクセスしたすべてのページにキャッシュタグを追加する必要があります

CustomAttribute を作成して、すべてのページにこれを追加し、[NoCache]装飾します

public class NoCacheAttribute : ActionFilterAttribute
{  
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);            
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}


public class AccountController : Controller
{
    [NoCache]
    public ActionResult Logout()
    {
        return View();
    }
}

または、次のようなページでjavascriptで試してください

<SCRIPT type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</SCRIPT>

<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">
于 2013-10-11T10:23:59.967 に答える