繁体   English   中英

单击后退按钮时,注销后页面仍显示 asp.net mvc 3

[英]Page still shows after logout when back button is clicked asp.net mvc 3

我见过很多与此几乎相似的问题。 但是,我还没有找到可以解决我的问题的答案。

我有一个注销按钮,我使用 Session.Abandon() 和 Session.Clear() 清除 session。它工作正常。 但是,每当我点击浏览器上的后退按钮时,页面仍在显示。 但是,它应该显示登录表单,因为用户已经注销。

Controller:

[HttpPost]
public ActionResult LogOut()
{
     Session.Clear();
     Session.Abandon();
     return RedirectToAction("Index", "LogIn");
}

如何解决这个问题?非常感谢任何建议。 提前致谢。

您可以在global.asax设置NoCache

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

您可以将其设置为“ServerAndNoCache”以强制浏览器不缓存页面而不是服务器来缓存页面,因此服务器上没有额外的负载。

在另一个线程中,我在 ASP.NET MVC 中获得了此 Prevent Caching 的答案,用于使用属性的特定操作

我的解决方案(.Net 6 MVC)如下:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System;

namespace YourSolutionName.Web.Mvc.Controllers.Attributes
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.GetTypedHeaders().CacheControl =
                    new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        NoStore = true,
                        NoCache = true,
                    };

            base.OnResultExecuting(filterContext);
        }
    }
}

然后将 [NoCache] 添加到我想要的控制器。

我选择它是因为它可以更好地控制我想禁用缓存的位置,但是如果您想为整个解决方案这样做,可以使用中间件(在 Startup.cs 上) https://learn.microsoft .com/zh-cn/as.net/core/performance/caching/middleware?view=as.netcore-7.0

            app.UseResponseCaching();
            app.Use(async (context, next) =>
            {
                context.Response.GetTypedHeaders().CacheControl =
                    new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        NoStore = true,
                        NoCache = true,
                    };

                await next();
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM