The MetadataExample project demonstrates how to retrieve X.509 certificates from SAML metadata.
The relevant code from the MetadataExample project is shown below.
// Reads the X.509 certificates contained within an IdP or SP SSO descriptor
private static void ReadX509Certificates(RoleDescriptorType roleDescriptor) {
foreach (KeyDescriptor keyDescriptor in roleDescriptor.KeyDescriptors) {
KeyInfo keyInfo = new KeyInfo();
keyInfo.LoadXml(keyDescriptor.KeyInfo);
IEnumerator enumerator = keyInfo.GetEnumerator(typeof(KeyInfoX509Data));
while (enumerator.MoveNext()) {
KeyInfoX509Data keyInfoX509Data = (KeyInfoX509Data)enumerator.Current;
foreach (X509Certificate2 x509Certificate in keyInfoX509Data.Certificates) {
Console.WriteLine("X509 certificate: " + x509Certificate.ToString());
}
}
foreach (XmlElement xmlElement in keyDescriptor.EncryptionMethods) {
Console.WriteLine("Encryption method: " + KeyDescriptor.GetEncryptionMethodAlgorithm(xmlElement));
}
}
}