Пример #1
0
        public static XElement ToXElement(this Saml2Statement statement)
        {
            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }

            if (statement is Saml2AttributeStatement)
            {
                var attributeStatement = statement as Saml2AttributeStatement;
                var element            = new XElement(Saml2Namespaces.Saml2 + "AttributeStatement");

                foreach (var attribute in attributeStatement.Attributes)
                {
                    var attributeElement = new XElement(Saml2Namespaces.Saml2 + "Attribute", new XAttribute("Name", attribute.Name));

                    attributeElement.AddAttributeIfNotNullOrEmpty("FriendlyName", attribute.FriendlyName);
                    attributeElement.AddAttributeIfNotNullOrEmpty("NameFormat", attribute.NameFormat);
                    attributeElement.AddAttributeIfNotNullOrEmpty("OriginalIssuer", attribute.OriginalIssuer);

                    foreach (var value in attribute.Values)
                    {
                        attributeElement.Add(new XElement(Saml2Namespaces.Saml2 + "AttributeValue", value));
                    }

                    element.Add(attributeElement);
                }

                return(element);
            }
            else
            {
                throw new ArgumentNullException("statement");
            }
        }
        public void Saml2StatementExtensions_ToXElement_NullCheck()
        {
            Saml2Statement assertion = null;

            Action a = () => assertion.ToXElement();

            a.ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("statement");
        }
        /// <summary>
        /// Writes out the statement as an XElement.
        /// </summary>
        /// <param name="statement">Statement to create xml for.</param>
        /// <returns>XElement</returns>
        public static XElement ToXElement(this Saml2Statement statement)
        {
            if (statement == null)
            {
                throw new ArgumentNullException(nameof(statement));
            }

            var attributeStatement = statement as Saml2AttributeStatement;

            if (attributeStatement != null)
            {
                return(ToXElement(attributeStatement));
            }

            var authnStatement = statement as Saml2AuthenticationStatement;

            if (authnStatement != null)
            {
                return(ToXElement(authnStatement));
            }

            throw new NotImplementedException("Statement of type " + statement.GetType().Name + " is not supported.");
        }
        /// <summary>
        /// Writes a Saml2Statement.
        /// </summary>
        /// <remarks>
        /// This method may write a &lt;saml:AttributeStatement>, &lt;saml:AuthnStatement> 
        /// or &lt;saml:AuthzDecisionStatement> element. To handle custom Saml2Statement
        /// classes for writing a &lt;saml:Statement> element, override this method.
        /// </remarks>
        /// <param name="writer">A <see cref="XmlWriter"/> to serialize the <see cref="Saml2Statement"/>.</param>
        /// <param name="data">The <see cref="Saml2Statement"/> to serialize.</param>
        protected virtual void WriteStatement(XmlWriter writer, Saml2Statement data)
        {
            if (null == writer)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
            }

            if (null == data)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("data");
            }

            Saml2AttributeStatement attributeStatement = data as Saml2AttributeStatement;
            if (null != attributeStatement)
            {
                this.WriteAttributeStatement(writer, attributeStatement);
                return;
            }

            Saml2AuthenticationStatement authnStatement = data as Saml2AuthenticationStatement;
            if (null != authnStatement)
            {
                this.WriteAuthenticationStatement(writer, authnStatement);
                return;
            }

            Saml2AuthorizationDecisionStatement authzStatement = data as Saml2AuthorizationDecisionStatement;
            if (null != authzStatement)
            {
                this.WriteAuthorizationDecisionStatement(writer, authzStatement);
                return;
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                new InvalidOperationException(SR.GetString(SR.ID4107, data.GetType().AssemblyQualifiedName)));
        }