我已經看過關於這個問題的其他文章,並沒有一個似乎解決了我的情況。SignedXml checksignature返回false
我一直在嘗試驗證上週的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是一個SignedXml對象使用新的SignedXml創建(xmlElement)
這兩個客戶端都會通過checkignature返回false,但是當我使用我的證書創建自己的簽名saml時,它將返回true。
我在這裏錯過了什麼?
編輯:有兩個客戶都是在Java和我張貼的SetSigningKeyFromKeyInfo方法
讓我猜測,您收到的斷言是以非.NET語言生成的,如Java? – 2010-10-14 17:11:39
什麼'SetSigningKeyFromKeyInfo(signedXml);'做什麼? – 2010-10-14 17:13:36
當您對base64進行解碼時,您是否可以將xml轉儲到一個文件並將其與您自己的斷言之一進行比較以檢查(微妙的)結構不一致? – 2010-10-14 17:17:16