Code: Select all
signedXmlWithSchPrefix.SetAttribute("xmlns:sch", customNamespace);
Hier ist meine aktuelle Implementierung: < /p>
Code: Select all
public static string SignWithCertificate(string xmlFilePath, string certThumbprint)
{
// Busca o certificado
X509Certificate2 cert = GetCertificateFromLocalMachine(certThumbprint);
if (cert == null || !cert.HasPrivateKey)
throw new Exception("Certificado não encontrado.");
// Faz o parse do XML
XmlDocument xmlDoc = new() { PreserveWhitespace = true };
xmlDoc.LoadXml(File.ReadAllText(xmlFilePath));
if (xmlDoc.DocumentElement == null)
throw new ArgumentException("Falta o elemento raíz do documento XML.");
// Define o prefixo e namespace no documento ANTES de assinar
string customPrefix = "sch";
string customNamespace = "http://www.ans.gov.br/padroes/tiss/schemas";
XmlNamespaceManager nsManager = new(xmlDoc.NameTable);
nsManager.AddNamespace(customPrefix, customNamespace);
// Adiciona as referências/transforms
Reference reference = new() { Uri = "" };
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
// Prepara a assinatura
SignedXml signedXml = new(xmlDoc) { SigningKey = cert.GetRSAPrivateKey() };
Signature xmlSignature = signedXml.Signature;
xmlSignature.SignedInfo.AddReference(reference);
KeyInfo keyInfo = new();
keyInfo.AddClause(new KeyInfoX509Data(cert));
xmlSignature.KeyInfo = keyInfo;
// Gera a assinatura
signedXml.ComputeSignature();
XmlElement signedXmlWithSchPrefix = signedXml.GetXml();
signedXmlWithSchPrefix.SetAttribute("xmlns:sch", customNamespace); //