登录和注销后 MVC 6 中的后退按钮问题

Back button issue in MVC 6 after login and logout

我试图限制用户在 Login/Logout 进入应用程序后单击后退按钮。

如果用户登录到应用程序,则在单击后退按钮后不应显示登录视图,如果用户已从应用程序注销,则不应显示单击后退按钮注销视图。

我已经使用了 link 数量中给出的缓存技术,但它似乎不起作用。

这个CacheAfterLogoutlink照原样做,问题还是没有解决。我正在使用 asp.net 身份框架。

有人可以帮忙吗?

只需将这些行添加到您的 Global.asax.cs 文件中。

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

要禁用缓存,您可以将以下内容应用于您希望禁用缓存的每个控制器

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]

您还可以设置 named cache profiles 并在运行时配置设置。

可以通过在您希望禁用缓存的每个控制器上应用 [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] 来禁用缓存。

通过在 startup.cs 中添加您的自定义过滤器,同时使用 ASP.Net Core 1.0.-* 和 MVC6 配置 MVC 服务,这可以应用于全局所有控制器。

首先创建一个实现 ResponseCacheAttribute 的自定义缓存过滤器,如下所示

public class ResponseCacheFilter : ResponseCacheAttribute
{
    public ResponseCacheFilter() : base()
    {
        Location = ResponseCacheLocation.None;
        NoStore = true;
    }
}

这应该添加到 startup.cs 文件中,如下所示。

public void ConfigureServices(IServiceCollection services)
{
   // Add framework and custom services.
   services.AddMvc(config =>
     {
         config.Filters.Add(new ResponseCacheFilter());
     });
}

https://docs.asp.net/en/latest/performance/caching/response.html#responsecache-attribute

查看ResponseCache的配置

只需在 Global.asax.cs

中添加此代码
protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
    }