Skip to content
Snippets Groups Projects
Select Git revision
  • v2
  • Coscine/FederationAdaption default
  • gitkeep
  • dev protected
  • Sprint/2022-01
  • fixmerge
  • master
  • develop protected
  • dependabot/nuget/Samples/SampleMvcApplication/bootstrap-3.4.1
  • dependabot/nuget/Samples/SampleOwinApplication/bootstrap-3.4.1
  • v1
  • netcore-cookiemanager
  • limitingcookiemanager
  • owin-cookiemanager
  • owin-cookiemanger-changeinterface
  • excess-cookies
  • missing_nodes_exceptions
  • empty_ref_exception
  • fix_shared_options
  • csphashsupport
  • v2.7.0
  • v1.0.2
  • v2.6.0
  • v2.5.0
  • v1.0.1
  • v2.4.0
  • v2.3.0
  • v2.2.0
  • v2.1.0
  • v2.0.0
  • v1.0.0
  • v0.24.0
  • v0.23.0
  • v2.0.0-preview01
  • v0.22.0
  • v0.21.2
  • v0.21.1
  • v0.21.0
  • v0.20.0
  • v0.19.0
40 results

Saml2SubjectExtensions.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Saml2SubjectExtensions.cs 3.76 KiB
    using Microsoft.IdentityModel.Tokens.Saml2;
    using System;
    using System.Xml.Linq;
    
    namespace Sustainsys.Saml2
    {
        /// <summary>
        /// Extension methods for Saml2Subject
        /// </summary>
        public static class Saml2SubjectExtensions
        {
            /// <summary>
            /// Writes out the subject as an XElement.
            /// </summary>
            /// <param name="subject">The subject to create xml for.</param>
            /// <returns>XElement</returns>
            public static XElement ToXElement(this Saml2Subject subject)
            {
                if (subject == null)
                {
                    throw new ArgumentNullException(nameof(subject));
                }
    
                var element = new XElement(Saml2Namespaces.Saml2 + "Subject",
                    subject.NameId.ToXElement());
    
                foreach (var subjectConfirmation in subject.SubjectConfirmations)
                {
                    element.Add(subjectConfirmation.ToXElement());
                }
    
                if (subject.SubjectConfirmations.Count == 0)
                {
                    // Although SubjectConfirmation is optional in the SAML core spec, it is
                    // mandatory in the Web Browser SSO Profile and must have a value of bearer.
                    element.Add(new Saml2SubjectConfirmation(
                        new Uri("urn:oasis:names:tc:SAML:2.0:cm:bearer")).ToXElement());
                }
    
                return element;
            }
    
            /// <summary>
            /// Writes out the subject confirmation as an XElement.
            /// </summary>
            /// <param name="subjectConfirmation"></param>
            /// <returns></returns>
            /// <exception cref="ArgumentNullException"></exception>
            public static XElement ToXElement(this Saml2SubjectConfirmation subjectConfirmation)
            {
                if (subjectConfirmation == null)
                {
                    throw new ArgumentNullException(nameof(subjectConfirmation));
                }
    
                var element = new XElement(Saml2Namespaces.Saml2 + "SubjectConfirmation",
                    new XAttribute("Method", subjectConfirmation.Method.OriginalString));
    
                if (subjectConfirmation.SubjectConfirmationData != null)
                {
                    element.Add(subjectConfirmation.SubjectConfirmationData.ToXElement());
                }
    
                return element;
            }
    
            /// <summary>
            /// Writes out the subject confirmation data as an XElement.
            /// </summary>
            /// <param name="subjectConfirmationData"></param>
            /// <returns></returns>
            /// <exception cref="ArgumentNullException"></exception>
            public static XElement ToXElement(this Saml2SubjectConfirmationData subjectConfirmationData)
            {
                if (subjectConfirmationData == null)
                {
                    throw new ArgumentNullException(nameof(subjectConfirmationData));
                }
    
                var element = new XElement(Saml2Namespaces.Saml2 + "SubjectConfirmationData");
    
                if (subjectConfirmationData.NotOnOrAfter.HasValue)
                {
                    element.SetAttributeValue("NotOnOrAfter",
                        subjectConfirmationData.NotOnOrAfter.Value.ToSaml2DateTimeString());
                }
    
                if (subjectConfirmationData.InResponseTo != null)
                {
                    element.SetAttributeValue("InResponseTo", subjectConfirmationData.InResponseTo.Value);
                }
    
                if (subjectConfirmationData.Recipient != null)
                {
                    element.SetAttributeValue("Recipient", subjectConfirmationData.Recipient.OriginalString);
                }
    
                if (subjectConfirmationData.NotBefore.HasValue)
                {
                    element.SetAttributeValue("NotBefore",
                        subjectConfirmationData.NotBefore.Value.ToSaml2DateTimeString());
                }
    
                return element;
            }
        }
    }