Getting Invalid Form Key. Please refresh the page error not able to do any action after upgrade of Magento 2.2.5 to Magento 2.3.
-
Check this out simicart.com/blog/magento-2-invalid-form-key– Prathap GunasekaranCommented Feb 13, 2019 at 8:15
-
Are you on local server ? like xampp ?– anonymousCommented Feb 13, 2019 at 8:19
-
Yes, im using xampp for local– Bangaram sandhyaCommented Feb 13, 2019 at 8:21
-
2What's your base Url ? If its localhost/your-project-name than you can give a try to replace localhost with 127.0.0.1 Run setup:upgrade It should solve your issue.– anonymousCommented Feb 13, 2019 at 8:28
-
i don't think so its a server issue, because till now i'm using magento 2.2.5 there is no issues when i upgraded to 2.3 then only this issue came.– Bangaram sandhyaCommented Feb 13, 2019 at 9:49
2 Answers
Finally I got the solution
I have a custom payment method that uses the cc-form to take credit card payments with and without 3dsecure. After placing order with 3dsecure, I am re-directing to 3dsecure page as normal, but on returning from 3dsecure, I am re-directed to the home page with "Invalid Form Key. Please refresh the page why because in Magento 2.3 core payment methods are using CsrfAwareActionInterface for each controller."
So now i have implemented same thing in my custom payment method as below
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
class CustomPaymentResponse extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface
/**
* @inheritDoc
*/
public function createCsrfValidationException(
RequestInterface $request
): ?InvalidRequestException {
return null;
}
/**
* @inheritDoc
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
Note : you can get reference from core module. Here is the core file path vendor\magento\module-authorizenet\Controller\Directpost\Payment\BackendResponse.php.
-
I have tried this but this is not working with me . I used a custom payment method (paytab) and the call back url from the payment method is giving me a 302 found and redirects to home page with the same message.– MelvinCommented Apr 24, 2019 at 7:35
-
-
3validateForCsrf is there for security reasons! you must not always return true! Commented Nov 5, 2019 at 13:21
-
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
class CustomPaymentResponse extends \Magento\Framework\App\Action\Action implements CsrfAwareActionInterface
/**
* @inheritDoc
*/
public function createCsrfValidationException(
RequestInterface $request
): ?InvalidRequestException {
return null;
}
/**
* @inheritDoc
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
/**
* Dispatch request
*
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function execute()
{
//your response check
}
Your response controller should be like this, then only form key issue will fix.
Referrence: https://github.com/magento/magento2/issues/19712