此错误通常发生在使用SAML响应进行身份验证时,可能存在SAML响应的签名验证失败。如果ADFS检测到签名验证失败,则会返回错误消息"Error with signature verification"。解决此问题的一种方法是检查SAML响应的签名配置是否正确,并确保签名证书已正确安装。以下是一个示例代码,通过此示例,您可以测试您的SAML响应是否能够通过签名验证。
using System;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.IdentityModel.Protocols.Saml2;
using Microsoft.IdentityModel.Tokens;
namespace SamlValidator
{
class Program
{
static void Main(string[] args)
{
//SAML Response string
string xmlString = "... ";
//Extract SAML Response XML
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
//Load Signing Certificate
X509Certificate2 certificate = new X509Certificate2(@"C:\Certificates\Signing.pfx", "password");
//Read and deserialize SAML Response
Saml2SecurityTokenHandler handler = new Saml2SecurityTokenHandler();
XmlReader reader = new XmlNodeReader(xmlDoc);
Saml2SecurityToken token = (Saml2SecurityToken)handler.ReadToken(reader);
//Validate SAML Response Signature
SecurityToken validatedToken;
TokenValidationParameters validationParameters = new TokenValidationParameters
{
ValidIssuer = "ADFS Issuer URI",
IssuerSigningKey = new X509SecurityKey(certificate.PublicKey),
ValidateAudience = false,
ValidateIssuerSigningKey = true,
ValidateLifetime = true
};
handler.ValidateToken(token, validationParameters, out validatedToken);
Console.WriteLine("SAML Response Signature Validation: PASSED");
}
}
}