Guest User

Untitled

a guest
Jul 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. public static bool Verify(X509Certificate2 cert, XmlElement xmlElement, SignedXml signedXml)
  2. {
  3. bool flag;
  4. try
  5. {
  6. KeyInfo keyInfo = new KeyInfo();
  7. var clause = new KeyInfoX509Data(cert);
  8. keyInfo.AddClause(clause);
  9.  
  10. XmlElement signatureElement = GetSignatureElement(xmlElement);
  11. if (signatureElement == null)
  12. {
  13. string message = "The XML does not contain a signature.";
  14. throw new SAMLSignatureException(message);
  15. }
  16. signedXml.LoadXml(signatureElement);
  17. if (keyInfo != null)
  18. {
  19. signedXml.KeyInfo = keyInfo;
  20. }
  21. SetSigningKeyFromKeyInfo(signedXml);
  22. flag = signedXml.CheckSignature(cert.PublicKey.Key);
  23. }
  24. catch (Exception exception)
  25. {
  26. throw new SAMLSignatureException("Failed to verify the XML signature.", exception);
  27. }
  28. return flag;
  29. }
  30.  
  31. private static void SetSigningKeyFromKeyInfo(SignedXml signedXml)
  32. {
  33. IEnumerator enumerator = signedXml.KeyInfo.GetEnumerator();
  34. while (enumerator.MoveNext())
  35. {
  36. if (enumerator.Current is KeyInfoX509Data)
  37. {
  38. var current = (KeyInfoX509Data) enumerator.Current;
  39. if (current.Certificates.Count != 0)
  40. {
  41. var certificate = (X509Certificate) current.Certificates[0];
  42. var certificate2 = new X509Certificate2(certificate);
  43. AsymmetricAlgorithm key = certificate2.PublicKey.Key;
  44. signedXml.SigningKey = key;
  45. return;
  46. }
  47. }
  48. else
  49. {
  50. if (enumerator.Current is RSAKeyValue)
  51. {
  52. var value2 = (RSAKeyValue) enumerator.Current;
  53. signedXml.SigningKey = value2.Key;
  54. return;
  55. }
  56. if (enumerator.Current is DSAKeyValue)
  57. {
  58. var value3 = (DSAKeyValue) enumerator.Current;
  59. signedXml.SigningKey = value3.Key;
  60. return;
  61. }
  62. }
  63. }
  64. throw new SAMLSignatureException("No signing key could be found in the key info.");
  65. }
  66.  
  67. var saml = System.Text.Encoding.Default.GetString(Convert.FromBase64String(samlToken))
  68.  
  69. var saml = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(samlToken))
Add Comment
Please, Sign In to add comment