[Quick Fix] Magento 2: ‘Invalid Form Key. Please refresh the page’ error
If you receive a “Invalid Form Key. Please refresh the page'” error after logging in to the Magento admin dashboard and are looking for a quick solution. You are in the correct location, and I have a simple fix for you.

The following are some of your activities on the Magento 2 store that led to this error:
- After you have upgraded to the Magento 2 version.
- When you upgrade a Magento 2 extension or theme.
- When you design a product that can be customized and has various attributes.
- Add to the cart on localhost when creating an account or signing up.
- Whenever you add a product to your wish list.
You can easily fix this error by following the below steps.
- Open the file <Magento installation directory>/vendor/magento/module-backend/App/Action/Plugin/Authentication.php
Please take a backup of the file before making any changes.
- The following lines in the file should be commented out.
$requestParts = explode('/', trim($request->getRequestUri(), '/'), 3);
$baseUrlPath = trim(parse_url($this->backendUrl->getBaseUrl(), PHP_URL_PATH), '/');
$routeIndex = empty($baseUrlPath) ? 0 : 1;
$requestUri = $this->_url->getUrl($requestParts[$routeIndex]);
- Add the following line to the file
$requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl());
Here is the output:
// Checks, whether secret key is required for admin access or request uri is explicitly set
if ($this->_url->useSecretKey()) {
# $requestParts = explode('/', trim($request->getRequestUri(), '/'), 3);
# $baseUrlPath = trim(parse_url($this->backendUrl->getBaseUrl(), PHP_URL_PATH), '/');
# $routeIndex = empty($baseUrlPath) ? 0 : 1;
# $requestUri = $this->_url->getUrl($requestParts[$routeIndex]);
$requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl());
} elseif ($request) {
$requestUri = $request->getRequestUri();
}
if (!$requestUri) {
return false;
That’s it 🙂 Please refresh the Magento admin panel Dashboard and check it again.