注销Aspnet Core后如何防止浏览器后退按钮

jol*_*ice 2 authentication cookies core logoff back

我有一个带有 cookie 身份验证的 aspnet 核心网站。当我注销,然后,当我单击浏览器的后退按钮时,我导航到最后一个网页,但我不希望那样,我不想将用户重定向到登录页面进行身份验证再次。

我的启动.cs

public void ConfigureServices(IServiceCollection services)
        {
          ....
            services.AddIdentity<ApplicationUser, ApplicationRole>(
            config =>
            {
                config.User.RequireUniqueEmail = true;
                config.SignIn.RequireConfirmedEmail = true;
                config.Password.RequiredLength = 8;
                config.Cookies.ApplicationCookie.LoginPath = "/Home/Login";
            })
            .AddEntityFrameworkStores<DbContext>()
            .AddDefaultTokenProviders();
        ......
        }
Run Code Online (Sandbox Code Playgroud)

我的控制器.cs

 public class HomeController : Controller
    {
        .....
        private readonly string _externalCookieScheme;
        ....


        public HomeController(
           .....
            IOptions<IdentityCookieOptions> identityCookieOptions,
            .....)
        {
            ....
            _externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
            ....

        }




        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Login()
        {
            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> LogOff()
        {
            await HttpContext.Authentication.SignOutAsync(_externalCookieScheme); //don´t remove the cookie
            _logger.LogInformation(4, "User logged out.");
            return RedirectToAction(nameof(HomeController.Login), "Home");
        }       
}
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

此致。

乔利尼斯

Ope*_*ity 5

您需要设置 Cache-Control 标头。对于单个页面或控制器,您可以像这样设置标题:

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请确保标题没有被覆盖。您可以在我的博客文章中找到详细说明:如何在 ASP.NET Core MVC 中注销后防止返回按钮

  • 请注意,如果您想宣传自己的产品/博客,**必须公开您的隶属关系**,否则您的回答可能会被标记为垃圾邮件。请阅读[如何成为垃圾邮件发送者](https://stackoverflow.com/help/promotion) (3认同)