XmlWriter 不会正确生成我需要的名称空间

XmlWriter won't generate the namespaces I need correctly

我正在尝试生成 XML 符合此格式的数据:

<samlp:AuthnRequest
        xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
        xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
        IssueInstant="2018-07-04T19:19:53.284Z"
        ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
        Version="2.0">
    <samlp:NameIDPolicy
            AllowCreate="true"
            Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"/>
</samlp:AuthnRequest>

由于匿名原因,显然缺少很多标签。

我正在使用 XmlWriter

我设法用不同的 WriteStartElement() 和 WriteAttributeString() 集生成了这个数据,但我永远做不到。结果要么略有不同,要么只是在中途崩溃并出现 XmlException,如

System.Xml.XmlException: 'The prefix '' cannot be redefined from 'samlp' to
'urn:oasis:names:tc:SAML:2.0:assertion' within the same start element tag.'

这是我的一些尝试和结果。

尝试 #1

using (XmlWriter xw = XmlWriter.Create(sw, xws))
{
    xw.WriteStartElement("AuthnRequest", "samlp");
    xw.WriteAttributeString("xmlns", "samlp", null, "urn:oasis:names:tc:SAML:2.0:protocol");
    xw.WriteAttributeString("xmlns", "urn:oasis:names:tc:SAML:2.0:assertion");
    // ...
}

结果 #1

Crashed with the above message.

尝试 #2

using (XmlWriter xw = XmlWriter.Create(sw, xws))
{
    xw.WriteStartElement("AuthnRequest", "samlp");
    xw.WriteAttributeString("xmlns", "samlp", null, "urn:oasis:names:tc:SAML:2.0:protocol");
    // ...
}

结果 #2

<AuthnRequest
    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
    ForceAuthn="false" ID="ID_4f85b6d1-a839-4899-972c-12275bf8711c"
    IssueInstant="2018-08-15T18:23:49Z"
    ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
    Version="2.0"
    xmlns="samlp">
    ...

其余的尝试都是这两个的细微变化,围绕参数移动,但 none 与我想要的结果一样接近。

具体来说,我在生成这个属性时遇到了很多麻烦:

xmlns="urn:oasis:names:tc:SAML:2.0:assertion"

它没有显示或错误。

我无法弄清楚我缺少什么才能按照我需要的方式生成它。

以下代码有效并成功写入您需要的XML:

var issueInstant = DateTime.Parse("2018-07-04T19:19:53.284Z", CultureInfo.InvariantCulture);

using (var xw = XmlWriter.Create(sw, xws))
{
    var samplNs = "urn:oasis:names:tc:SAML:2.0:protocol";
    var defaultNs = "urn:oasis:names:tc:SAML:2.0:assertion";

    // XmlWriter.WriteStartElement(String prefix, String localName, String ns)
    xw.WriteStartElement("samlp", "AuthnRequest", samplNs);

    // Write the xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" namespace.
    // Actually this is redundant as the attribute will be automatically be written at the end of the
    // attribute list do to the previous call to xw.WriteStartElement().  Call it here only if, for some
    // reason, you need to control the attribute order.
    xw.WriteAttributeString("xmlns", "samlp", null, samplNs);               

    // Write the default namespace
    xw.WriteAttributeString("xmlns", defaultNs);

    // Write attribute values.              
    xw.WriteAttributeString("IssueInstant", XmlConvert.ToString(issueInstant, XmlDateTimeSerializationMode.Utc));
    xw.WriteAttributeString("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    xw.WriteAttributeString("Version", "2.0");

    // Write samlp:NameIDPolicy
    // No need to specify prefix since it is specified in the document root.
    xw.WriteStartElement("NameIDPolicy", samplNs);
    xw.WriteAttributeString("AllowCreate", XmlConvert.ToString(true));
    xw.WriteAttributeString("Format", "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified");

    // Write the end of NameIDPolicy
    xw.WriteEndElement();

    // Write the end of AuthnRequest
    xw.WriteEndElement();
}       

备注:

  • 方法 XmlWriter.WriteStartElement(String localName, String ns) 命名空间 作为第二个参数,但您传递的是命名空间 前缀 "samlp"。所以那是行不通的。

  • 但是调用

    xw.WriteStartElement("AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol"); 
    

    也会失败,因为这样做会将 "urn:oasis:names:tc:SAML:2.0:protocol" 建立为 default 命名空间,而您希望它成为前缀为 [=15= 的非默认命名空间].因此必须使用XmlWriter.WriteStartElement(String prefix, String localName, String ns)

  • 明确地编写了带有指定名称空间和名称空间前缀的 AuthnRequest 元素后,实际上不再需要编写名称空间属性了——XmlWriter 就可以了它会自动出现在属性列表的末尾。如果你想控制它在属性列表中的位置,你只需要手动编写命名空间属性。但是,根据 XML Standard:

    Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

    所以,你可以跳过那个。

  • XmlConvertclass 中的方法可用于将非字符串基元从 XML 正确转换为 XML。

示例 fiddle 此处:https://dotnetfiddle.net/99hu1I.

我发现当你有复杂的命名空间时,只解析一个字符串要容易得多。使用 xml linq :

            string xml =
                "<samlp:AuthnRequest" +
                    " xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\"" +
                    " xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\"" +
                    " IssueInstant=\"2018-07-04T19:19:53.284Z\"" +
                    " ProtocolBinding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\"" +
                    " Version=\"2.0\">" +
                    "<samlp:NameIDPolicy" +
                       " AllowCreate=\"true\"" +
                       " Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\"/>" +
                "</samlp:AuthnRequest>";

            XDocument doc = XDocument.Parse(xml);