diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
index 2ec89c801119030d8af5cbe42d673eff6cc01855..07ea1ba7a288a2d8834df658317d51aad6cb017a 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
@@ -172,8 +172,19 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource {
 
 		$metadataSet = $this->getMetadataSet($set);
 
-		foreach($metadataSet AS $index => $entry)
-			if ($entry['entityid'] === $entityId) return $index;
+		/* Check for hostname. */
+		$currenthost = SimpleSAML_Utilities::getSelfHost(); // sp.example.org
+		if(strpos($currenthost, ":") !== FALSE) {
+			$currenthostdecomposed = explode(":", $currenthost);
+			$currenthost = $currenthostdecomposed[0];
+		}
+		
+		foreach($metadataSet AS $index => $entry) {
+			if ($index === $entityId) return $index;
+			if ($entry['entityid'] === $entityId) {
+				if ($entry['host'] === '__DEFAULT__' || $entry['host'] === $currenthost) return $index;
+			}
+		}
 		
 		return NULL;
 	}