How to reliably obtain the certificate data from a SAML metadata file in .NET?
I want to parse SAML metadata. I’ve found that the System.IdentityModel.Metadata
namespace seems to contain useful classes for this.
var serializer = new MetadataSerializer(); var entityDescriptor = (EntityDescriptor)serializer.ReadMetadata(xmlString); var ssoDescriptor = entityDescriptor.RoleDescriptors.OfType<IdentityProviderSingleSignOnDescriptor>().First();
However, how do I reliably obtain the raw certificate data contained in the Keys? I need this to verify the certificate using some generic certificate checking code.
In some cases this seems to work:
var key = ssoDescriptor.Keys.First(); var clause = key.KeyInfo.OfType<X509RawDataKeyIdentifierClause>().First(); var certificateBytes = clause.GetX509RawData();
However, this only works if the KeyInfo is of type X509RawDataKeyIdentifierClause
. But I’ve found this to not always be the case. How do I obtain the raw data from say a X509IssuerSerialKeyIdentifierClause
?