我在这里查看了有关此问题的其他帖子,但似乎都没有解决我的情况。
上周我一直在尝试验证 SAML 断言,我有 2 个客户向我发送了 SAML,但我无法验证它。
主要过程是我们得到一个 base64 编码的断言,然后我对其进行解码。将其加载到 PreserveWhitespace = true 的 XmlDocment 中。
验证方法是
public static bool Verify(X509Certificate2 cert, XmlElement xmlElement, SignedXml signedXml)
{
bool flag;
try
{
KeyInfo keyInfo = new KeyInfo();
var clause = new KeyInfoX509Data(cert);
keyInfo.AddClause(clause);
XmlElement signatureElement = GetSignatureElement(xmlElement);
if (signatureElement == null)
{
string message = "The XML does not contain a signature.";
throw new SAMLSignatureException(message);
}
signedXml.LoadXml(signatureElement);
if (keyInfo != null)
{
signedXml.KeyInfo = keyInfo;
}
SetSigningKeyFromKeyInfo(signedXml);
flag = signedXml.CheckSignature(cert.PublicKey.Key);
}
catch (Exception exception)
{
throw new SAMLSignatureException("Failed to verify the XML signature.", exception);
}
return flag;
}
private static void SetSigningKeyFromKeyInfo(SignedXml signedXml)
{
IEnumerator enumerator = signedXml.KeyInfo.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current is KeyInfoX509Data)
{
var current = (KeyInfoX509Data) enumerator.Current;
if (current.Certificates.Count != 0)
{
var certificate = (X509Certificate) current.Certificates[0];
var certificate2 = new X509Certificate2(certificate);
AsymmetricAlgorithm key = certificate2.PublicKey.Key;
signedXml.SigningKey = key;
return;
}
}
else
{
if (enumerator.Current is RSAKeyValue)
{
var value2 = (RSAKeyValue) enumerator.Current;
signedXml.SigningKey = value2.Key;
return;
}
if (enumerator.Current is DSAKeyValue)
{
var value3 = (DSAKeyValue) enumerator.Current;
signedXml.SigningKey = value3.Key;
return;
}
}
}
throw new SAMLSignatureException("No signing key could be found in the key info.");
}
我有从 Web.Config 读入的客户端证书(它存储为 base64 编码字符串) xmlelement 是签名元素,signedXml 是使用 new SignedXml(xmlElement) 创建的 SignedXml 对象
两个客户端都通过 checksignature 返回错误,但是当我使用我的证书创建自己的签名 saml 时,它将返回 true。
我在这里想念什么?
编辑:是的,两个客户端都在 Java 上,我发布了 SetSigningKeyFromKeyInfo 方法