如何使用C#验证证书?

时间:2014-01-20 13:01:46

标签: c# saml-2.0

我正在使用visual studio 2005,我希望使用应用程序证书验证SAML响应证书,这里我从身份提供商那里收到SAML响应,并且发送带有证书的SAML响应,并且应用程序分别具有相同的证书,在这里我需要检查SAML响应是否具有SAML证书。你能帮助我吗? 谢谢, Gopi G

1 个答案:

答案 0 :(得分:0)

以下是如何验证完整SAML身份验证响应的签名的示例。断言签名验证类似。

const string XpathResponseSignatureCertificate = "/samlp:Response/ds:Signature/ds:KeyInfo/ds:X509Data/ds:X509Certificate";

XmlElement xmlResponseSignature =  GetSignatureElement(authenticationResponse);

// Get certificate from IdP metadata document
X509Certificate2 signingCertificate = identityProvider.SigningCertificate;

XmlDocument responseXmlDocument = GetResponseAsXmlDocument(string samlResponse);

XmlNode responseSignatureXmlNode = this.responseXmlDocument.DocumentElement.SelectSingleNode(XpathResponseSignatureCertificate, this.namespaceManager);
XmlElement xmlSignature = responseSignatureXmlNode .InnerText.Trim()

SignedXml signedXml = new SignedXml(ResponseXmlDocumen;
signedXml.LoadXml((XmlElement)xmlSignature);

if (signedXml.CheckSignature(cert, true) == false)
{
    throw new Exception("Not valid signature");
}

bool isReferenceValid = false;
foreach (Reference reference in signedXml.SignedInfo.References)
{
    string refValue = reference.Uri.Substring(1);
    if (refValue  == authenticationResponse.Id)
    {
        isReferenceValid = true;
    }
}

if (isReferenceValid == false)
{
    throw new Exception("Not valid signature reference");
}