/// <summary> /// Merges the modified attributes into <code>AttributeStatement</code> of the assertion. /// </summary> private void InsertAttributes() { if (_assertionAttributes == null) { return; } // Generate the new AttributeStatement var attributeStatement = new AttributeStatement(); var statements = new List <object>(_encryptedAssertionAttributes.Count + _assertionAttributes.Count); statements.AddRange(_assertionAttributes.ToArray()); statements.AddRange(_encryptedAssertionAttributes.ToArray()); attributeStatement.Items = statements.ToArray(); var list = XmlAssertion.GetElementsByTagName(AttributeStatement.ElementName, Saml20Constants.Assertion); if (list.Count > 0) { // Remove the old AttributeStatement. XmlAssertion.RemoveChild(list[0]); // FIX _samlAssertion.DocumentElement.RemoveChild(list[0]); } // Only insert a new AttributeStatement if there are attributes. if (statements.Count > 0) { // Convert the new AttributeStatement to the Document Object Model and make a silent prayer that one day we will // be able to make this transition in a more elegant way. var attributeStatementDoc = Serialization.Serialize(attributeStatement); var attr = XmlAssertion.OwnerDocument.ImportNode(attributeStatementDoc.DocumentElement, true); // Insert the new statement. XmlAssertion.AppendChild(attr); } _encryptedAssertionAttributes = null; _assertionAttributes = null; }
/// <summary> /// Extracts the list of attributes from the <AttributeStatement> of the assertion, and /// stores it in <code>_assertionAttributes</code>. /// </summary> private void ExtractAttributes() { _assertionAttributes = new List <SamlAttribute>(0); _encryptedAssertionAttributes = new List <EncryptedElement>(0); var list = XmlAssertion.GetElementsByTagName(AttributeStatement.ElementName, Saml20Constants.Assertion); if (list.Count == 0) { return; } // NOTE It would be nice to implement a better-performing solution where only the AttributeStatement is converted. // NOTE Namespace issues in the xml-schema "type"-attribute prevents this, though. var assertion = Serialization.Deserialize <Assertion>(new XmlNodeReader(XmlAssertion)); var attributeStatements = assertion.GetAttributeStatements(); if (attributeStatements.Count == 0 || attributeStatements[0].Items == null) { return; } var attributeStatement = attributeStatements[0]; foreach (var item in attributeStatement.Items) { if (item is SamlAttribute) { _assertionAttributes.Add((SamlAttribute)item); } if (item is EncryptedElement) { _encryptedAssertionAttributes.Add((EncryptedElement)item); } } }