2014-01-20 73 views
0

我正在使用visual studio 2005,并且希望验证具有应用程序证书的SAML响应证书,这里我从身份提供商那里得到SAML响应,并且它发送带有证书的SAML响应,并且应用程序单独拥有相同的证书,这里我需要检查SAML响应是否具有SAML证书。你能请任何人帮助我吗? With Thanks, Gopi G如何使用C#验证证书?

回答

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"); 
}