提取与X509证书XML文件中的公钥?
我想从XML文件创建在C#中的 X509Certificate2
对象。 XML文件是我们从供应商那里获得了 SAML
元数据文件。
I am trying to create an X509Certificate2
object in C# from an XML file. The XML file is a SAML
metadata file that we received from a vendor.
我试图提取公从这些XML元素键:
I am trying to extract the public key from these XML Elements:
<X509Data>
<X509Certificate>
MIIB7DCCAVmgAwIBAgIQPjHcBTL63bBLuJZ88RcrCjAJBgUrDgMCHQUAMBExDzANBgNVBAMT
BnJvbWVvazAgFw0xMDAzMTUwMjI1MjZaGA8yMTEwMDIxOTAyMjUyNlowETEPMA0GA1UEAxMG
cm9tZW9rMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAu/sBh13A27rR7gJpZsI6zCee
TXNohQWlq2z6Zg8Oxzsy5JoVV
</X509Certificate>
</X509Data>
有没有在C#的方式来提取无论是从XML元素的.CER文件或公钥呢?
Is there a way in C# to extract either the .cer file or public key from the XML element?
这是一个很难回答的问题不知道X509证书是如何编码的回答,但假设你有编码的东西,你可以做类似如下:
This is a difficult question to answer without knowing how the X509Certificate is encoded, but assuming you have the encoding stuff, you can do something like the following:
var document = new XmlDocument();
document.LoadXml(txtXml.Text);
var cert = document.SelectSingleNode("X509Data/X509Certificate").InnerText;
/*...Decode text in cert here (may need to use Encoding, Base64, UrlEncode, etc) ending with 'data' being a byte array...*/
var x509 = new X509Certificate2(data);
然后,你应该能够使用标准的文件I / O逻辑将文件写入到磁盘。
Then you should be able to write the file to disk using standard File I/O logic.