diff --git a/modules/core/www/loginuserpass.php b/modules/core/www/loginuserpass.php
index aa21292e6f4b42a1e522d20c1f880392744e8f40..87ccb70dce5eb9e2d5117a5d74e6dd4e9df7d0ba 100644
--- a/modules/core/www/loginuserpass.php
+++ b/modules/core/www/loginuserpass.php
@@ -15,6 +15,15 @@ if (!array_key_exists('AuthState', $_REQUEST)) {
 }
 $authStateId = $_REQUEST['AuthState'];
 
+try {
+	/* Retrieve the authentication state. */
+	$state = SimpleSAML_Auth_State::loadState($authStateId, sspmod_core_Auth_UserPassBase::STAGEID);
+} catch(Exception $e) {
+	if (array_key_exists('SessionLostURL', $_REQUEST)) {
+		SimpleSAML_Utilities::redirect($_REQUEST['SessionLostURL']);
+	}
+}
+
 if (array_key_exists('username', $_REQUEST)) {
 	$username = $_REQUEST['username'];
 } else {