我正在尝试生成符合这种格式的 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">
...
其余的尝试都是这两个的细微变化,围绕参数移动但没有一个像它们一样接近我想要的结果。
具体来说,我在生成这个属性时遇到了很多麻烦:
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)
将 命名空间 作为第二个参数,但您传递的是命名空间 prefix"samlp"
。所以那是行不通的。但是打电话
xw.WriteStartElement("AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
也会失败,因为这样做会将
"urn:oasis:names:tc:SAML:2.0:protocol"
建立为 默认 命名空间,而您希望它是带有前缀sampl
的非默认命名空间。因此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.
因此,您可以跳过它。
来自
XmlConvert
的方法类可用于正确地将非字符串基元与 XML 相互转换。
示例 fiddle 在这里:https://dotnetfiddle.net/99hu1I .
关于c# - XmlWriter 不会正确生成我需要的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51864429/