2017-05-29 152 views
1

我有一個aspnet核心網站,使用cookie身份驗證。 當我註銷時,然後當我點擊瀏覽器的後退按鈕時,我導航到最後一個網頁,我不想要那個,我不想讓用戶重定向到登錄頁面再次驗證。如何在註銷後防止瀏覽器後退按鈕Aspnet Core

我startup.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(); 
     ...... 
     } 

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

我是缺少在這裏?

此致敬禮。

jolynice

回答

2

您需要設置Cache-Control標頭。對於單個頁面或控制器,您可以像這樣設置標題:

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

如果這樣不起作用,請確保標題未被覆蓋。你可以在我的博客文章中找到詳細的解釋:How To Prevent the Back Button after Logout in ASP.NET Core MVC

+2

請注意,如果您想宣傳自己的產品/博客,您必須披露您的關聯**,否則您的答案可能被標記爲垃圾郵件。請閱讀[如何不成爲垃圾郵件發送者](https://stackoverflow.com/help/promotion) – DavidPostill

+0

感謝您的信息。我已經更新了答案。 – OpenIDAuthority

+0

OpenIdAuthority,感謝您的幫助。現在正在工作。 – jolynice