.net 8.0 - Going to previous screen when click on back button in browser - ASP.NET Core 8 MVC - Stack Overflow

admin2025-05-02  0

I have an ASP.NET Core 8 MVC web application. I have a scenario where when I click on the browser's back button, it goes to the previous screen. Here, the issue comes when I log out.

Suppose I am in a user profile view page and click on the logout button, then it will move to the login screen. But when the user clicks on the back arrow, it goes back to the previous screen. Here, it is the user profile view page. How can I solve this?

I have an ASP.NET Core 8 MVC web application. I have a scenario where when I click on the browser's back button, it goes to the previous screen. Here, the issue comes when I log out.

Suppose I am in a user profile view page and click on the logout button, then it will move to the login screen. But when the user clicks on the back arrow, it goes back to the previous screen. Here, it is the user profile view page. How can I solve this?

Share Improve this question edited Jan 2 at 12:22 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 2 at 12:05 MidlajMidlaj 213 bronze badges 1
  • Are you using ASP.NET Core Identity UI or a custom login/logout page with cookie authentication to implement authentication/authorization? – Zhi Lv Commented Jan 6 at 8:15
Add a comment  | 

1 Answer 1

Reset to default 0

1. Disable Browser Caching for Protected Pages

Prevent the browser from caching sensitive pages such as the user profile page. Use the appropriate HTTP headers to instruct the browser not to cache the page.

Add the following headers to your protected pages in your ASP.NET Core 8 MVC application:

Response.Headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0";
Response.Headers["Pragma"] = "no-cache";
Response.Headers["Expires"] = "-1";

Alternatively, create a reusable filter or middleware to apply these headers globally to protected pages:

public class NoCacheFilter : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext context)
    {
        context.HttpContext.Response.Headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0";
        context.HttpContext.Response.Headers["Pragma"] = "no-cache";
        context.HttpContext.Response.Headers["Expires"] = "-1";
        base.OnResultExecuting(context);
    }
}
转载请注明原文地址:http://anycun.com/QandA/1746121774a91972.html