格式错误的参考元素

Rus*_*uss 5 c# x509certificate xml-signature irs

我试图将引用添加到我的安全标题,并遇到一个相当普遍的错误:

格式错误的参考元素

我尝试了以下类似的结果:

  1. 通过使在参考所述文档内的元素ID的元素作为的URI所述的Reference目的.
  2. 传递一个XmlElement对象到Reference通过LoadXml()方法.我正在XmlElement使用此StackOverflow帖子GetIdElement上的重载找到检索引用.

当我传入一个空字符串作为时URI,ComputeSignature()方法SignedXml按预期工作.但是,我需要添加最多3个对安全标头的引用.

更新#1
感谢这篇博文,我能够从中创建简化版本,我相信导致我的问题的是使用Namespace属性和前缀.

更新#2
看起来好像元素Id属性上的命名空间声明<Timestamp>导致发生此错误.

更新#3
我认为我有这个工作.请参阅下面的回答帖子.

工作示例:
请注意,Id XAttribute定义的命名空间不起作用; 而Id XAttribute没有定义的命名空间确实有效.

private void CreateSecurityAndTimestampXML(string fileName)
{
    TimestampID = "TS-E" + GUID.NewGuid();
    DateTime SecurityTimestampUTC = DateTime.UtcNow;

    XDocument xdoc = new XDocument(
        new XElement(wsse + "Security",
            new XAttribute(XNamespace.Xmlns + "wsse", wsse.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "wsu", wsu.NamespaceName),
            new XElement(wsu + "Timestamp",
                // new XAttribute(wsu + "Id", TimestampID), // <-- Does Not Work
                new XAttribute("Id", TimestampID), // <-- Works
                new XElement(wsu + "Created", SecurityTimestampUTC.ToString(_timestampFormat)),
                new XElement(wsu + "Expires", SecurityTimestampUTC.AddMinutes(10).ToString(_timestampFormat))
            )
        )
    );

    using (XmlTextWriter tw = new XmlTextWriter(fileName, new UTF8Encoding(false)))
    {
        xdoc.WriteTo(tw);
    }
}

// Snippet
string[] elements = { TimestampID };

foreach (string s in elements)
{
    Reference reference = new Reference()
    {
        Uri = "#" + s
    };

    XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform();
    env.InclusiveNamespacesPrefixList = _includedPrefixList;
    reference.AddTransform(env);

    xSigned.AddReference(reference);
}

// Add Key Info Here.

// Compute the Signature.
xSigned.ComputeSignature();
Run Code Online (Sandbox Code Playgroud)

Rus*_*uss 7

看起来,至少在目前,实现这项工作的答案如下.

使用此处详述的SignedXml类,而不是使用类,创建一个新SignedXmlWithId对象.

// Create a new XML Document.
XmlDocument xdoc = new XmlDocument();

xdoc.PreserveWhitespace = true;

// Load the passed XML File using its name.
xdoc.Load(new XmlTextReader(fileName));

// Create a SignedXml Object.
//SignedXml xSigned = new SignedXml(xdoc);
SignedXmlWithId xSigned = new SignedXmlWithId(xdoc); // Use this class instead of the SignedXml class above.

// Add the key to the SignedXml document.
xSigned.SigningKey = cert.PrivateKey;
xSigned.Signature.Id = SignatureID;
xSigned.SignedInfo.CanonicalizationMethod = 
        SignedXml.XmlDsigExcC14NWithCommentsTransformUrl;

//Initialize a variable to contain the ID of the Timestamp element.
string elementId = TimestampID;

Reference reference = new Reference()
{
    Uri = "#" + elementId
};

XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform();
env.InclusiveNamespacesPrefixList = _includedPrefixList;
reference.AddTransform(env);

xSigned.AddReference(reference);

// Add Key Information (omitted)

// Compute Signature
xSigned.ComputeSignature();
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

4930 次

最近记录:

9 年,9 月 前