diff --git a/modules/saml/lib/IdP/SAML2.php b/modules/saml/lib/IdP/SAML2.php
index e5951696c25bad6879b65f81a03a166ad9732147..23696bf28632bf48e9319fa4114339f0c62fa37b 100644
--- a/modules/saml/lib/IdP/SAML2.php
+++ b/modules/saml/lib/IdP/SAML2.php
@@ -27,7 +27,6 @@ class sspmod_saml_IdP_SAML2 {
 
 		SimpleSAML_Logger::info('Sending SAML 2.0 Response to ' . var_export($spEntityId, TRUE));
 
-		$attributes = $state['Attributes'];
 		$requestId = $state['saml:RequestId'];
 		$relayState = $state['saml:RelayState'];
 		$consumerURL = $state['saml:ConsumerURL'];
@@ -46,7 +45,7 @@ class sspmod_saml_IdP_SAML2 {
 
 		$idpMetadata = $idp->getConfig();
 
-		$assertion = sspmod_saml2_Message::buildAssertion($idpMetadata, $spMetadata, $attributes, $consumerURL);
+		$assertion = sspmod_saml2_Message::buildAssertion($idpMetadata, $spMetadata, $state);
 		$assertion->setInResponseTo($requestId);
 		
 		if (isset($state['saml:AuthenticatingAuthority'])) {
diff --git a/modules/saml2/lib/Message.php b/modules/saml2/lib/Message.php
index 38de9951ff0df6511aa3bcc01f40060a8a2045e6..8e453037de2c6ec0cc2abf6e7b1fa9cf1a7b3abf 100644
--- a/modules/saml2/lib/Message.php
+++ b/modules/saml2/lib/Message.php
@@ -558,11 +558,13 @@ class sspmod_saml2_Message {
 	 *
 	 * @param SimpleSAML_Configuration $srcMetadata  The metadata of the sender (IdP).
 	 * @param SimpleSAML_Configuration $dstMetadata  The metadata of the recipient (SP).
-	 * @param array $attributes  The attributes of the user
+	 * @param array &$state  The state array with information about the request.
 	 * @return SAML2_Assertion  The assertion.
 	 */
 	public static function buildAssertion(SimpleSAML_Configuration $srcMetadata,
-		SimpleSAML_Configuration $dstMetadata, array $attributes, $consumerURL) {
+		SimpleSAML_Configuration $dstMetadata, array &$state) {
+		assert('isset($state["Attributes"])');
+		assert('isset($state["saml:ConsumerURL"])');
 
 		$signAssertion = $dstMetadata->getBoolean('saml20.sign.assertion', NULL);
 		if ($signAssertion === NULL) {
@@ -577,7 +579,7 @@ class sspmod_saml2_Message {
 		}
 
 		$a->setIssuer($srcMetadata->getString('entityid'));
-		$a->setDestination($consumerURL);
+		$a->setDestination($state['saml:ConsumerURL']);
 		$a->setValidAudiences(array($dstMetadata->getString('entityid')));
 
 		$a->setNotBefore(time() - 30);
@@ -608,7 +610,7 @@ class sspmod_saml2_Message {
 					'urn:oasis:names:tc:SAML:2.0:attrname-format:basic');
 			}
 			$a->setAttributeNameFormat($attributeNameFormat);
-			$attributes = self::encodeAttributes($srcMetadata, $dstMetadata, $attributes);
+			$attributes = self::encodeAttributes($srcMetadata, $dstMetadata, $state['Attributes']);
 			$a->setAttributes($attributes);
 		}
 
@@ -628,7 +630,7 @@ class sspmod_saml2_Message {
 		} else {
 		        /* this code will end up generating either a fixed assigned id (via nameid.attribute)
 			   or random id if not assigned/configured */
-			$nameIdValue = self::generateNameIdValue($srcMetadata, $dstMetadata, $attributes);
+			$nameIdValue = self::generateNameIdValue($srcMetadata, $dstMetadata, $state['Attributes']);
 		}
 
 		$a->setNameId(array(