0
我正在尝试在C#中为我尝试连接的Web服务构建SAML断言。我正在使用SamlAssertion
和X509 Cert作为SigningCredentials
C#中的SAML 1.1格式错误引用
这会生成一个SamlSecurityToken
,然后序列化。当我尝试验证签名时,我收到一条错误消息,指出
格式错误的参考元素。
代码将序列
var result = new StringBuilder();
using (var writer = XmlWriter.Create(result))
{
var serializer = new WSSecurityTokenSerializer(SecurityVersion.WSSecurity11, true);
serializer.WriteToken(writer, obj); //Obj is the SamlSecurityToken
}
var xml = result.ToString();
签名生成:
<Signature
xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></CanonicalizationMethod>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"></SignatureMethod>
<Reference URI="#_caeceed6-7006-4354-a398-75a8e4c52818">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"></Transform>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></Transform>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></DigestMethod>
<DigestValue>SOME VALUE</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>SOME VALUE</SignatureValue>
</Signature>
这几乎是我从网络服务提供者得到的例子。例子中的一部分:
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"></ds:Transform>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="code ds kind rw saml samlp typens #default xsd xsi"></ec:InclusiveNamespaces>
</ds:Transform>
的例子和生成的XML看起来几乎一样,除了一些前缀和InclusiveNamespaces标签。
我也看到了人们使用SignedXml
而不是WSSecurityTokenSerializer的例子,为什么?更多的控制生成的XML?或者我可以不使用WSSecurityTokenSerializer创建令牌?
很难说,没有看到整个生成的消息和堆栈跟踪。但也许它与前缀thingies有关?正如在http://stackoverflow.com/questions/5099156/malformed-reference-element-when-adding-a-reference-based-on-an-id-attribute-w – Thuan
通过重写GetIdElement我能够验证签名 –