После выхода из системы, если кнопка возврата в браузере нажмите, затем верните последний экран

Привет, я разрабатываю решение в MVC в первый раз, поэтому я столкнулся с большой проблемой, Когда я выхожу из своего приложения (веб-приложение mvc razor), он отображает страницу входа в систему, но если я нажму кнопку "Назад назад", отобразится последний экран, я не хочу этого, я хочу, если я нажму кнопку "Назад", он все равно отобразит ту же страницу входа. вот мой код для выхода из системы

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");
    }

Ответы

Ответ 1

У меня была эта проблема некоторое время назад, отключение кэша для всего приложения решило мою проблему, просто добавьте эту строку в файл Global.asax.cs

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

Надеюсь, что это поможет.

Ответ 2

Вам нужно добавить кеш 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.SetRevalidation(HttpCacheRevalidation.AllCaches);
        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="">

Ответ 3

protected void Application_BeginRequest()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            Response.Cache.SetNoStore();
        }

Add [Authorize] filter on each controller

Ответ 4

  protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
        string emailAddress = null;
        var cookie =Request.Cookies[FormsAuthentication.FormsCookieName];
        // Nothing to do if no cookie
        if (cookie != null)
        {
            // Decrypt the cookie
            var data = FormsAuthentication.Decrypt(cookie.Value);
            // Nothing to do if null
            if (data != null)
            {
                // Deserialize the custom data we stored in the cookie
                var o = JsonConvert.DeserializeObject<FormsAuthenticationTicketData>(data.UserData);

                // Nothing to do if null
                if (o != null)
                    emailAddress = o.EmailAddress;
            }
        }
        SetupAutoFac(emailAddress);
    }