diff --git a/www/saml2/idp/SSOService.php b/www/saml2/idp/SSOService.php
index f7657cd319714c05ecfdd6af69bf6253e7be3401..b145aa0d021e98b5954cfe5230e448090e0644b2 100644
--- a/www/saml2/idp/SSOService.php
+++ b/www/saml2/idp/SSOService.php
@@ -243,10 +243,6 @@ if($needAuth && !$isPassive) {
 		$spmetadata = $metadata->getMetaData($spentityid, 'saml20-sp-remote');
 		
 		$sp_name = (isset($spmetadata['name']) ? $spmetadata['name'] : $spentityid);
-	
-		// Adding this service provider to the list of sessions.
-		// Right now the list is used for SAML 2.0 only.
-		$session->add_sp_session($spentityid);
 
 		SimpleSAML_Logger::info('SAML2.0 - IdP.SSOService: Sending back AuthnResponse to ' . $spentityid);
 		
@@ -380,6 +376,11 @@ if($needAuth && !$isPassive) {
 		
 		
 		
+		// Adding this service provider to the list of sessions.
+		// Right now the list is used for SAML 2.0 only.
+		$session->add_sp_session($spentityid);
+		
+		
 		// Generate an SAML 2.0 AuthNResponse message
 		$ar = new SimpleSAML_XML_SAML20_AuthnResponse($config, $metadata);
 		$authnResponseXML = $ar->generate($idpentityid, $spentityid, $requestcache['RequestID'], null, $filteredattributes);