Occurs when the SAML object is about to create an XML element.

Syntax

public static event EventHandler<TEventArgs> CreateElement

Examples

Shows how to manipulate SAML XML by using a custom class or handling the CreateElement event.

using System;
using ComponentPro.Saml2;
using ComponentPro.Saml;
using System.Xml;
using System.Security.Cryptography.X509Certificates;

...

#region Using A Custom Class

class MySamlResponse : Response
{
    public override XmlElement GetXml(XmlDocument xmlDocument)
    {
        XmlElement xml = base.GetXml(xmlDocument);

        // Alter the xml here; 
        // Finding the element 'InclusiveNamespaces' by using the Select method. 
        XmlNode inclusiveNamespacesNode = xml.SelectSingleNode("xpath to your InclusiveNamespaces");
        xml.RemoveChild(inclusiveNamespacesNode);

        XmlNode keyInfoNode = xml.SelectSingleNode("xpath to your KeyInfo node");
        
        // Your node manipulation here. 
        // ...  
 
        return xml;
    }
}

static void AlterSamlAssertion_CustomClass()
{
    // Load the private key to sign the assertion. 
    X509Certificate2 x509Certificate = new X509Certificate2(@"..\..\Pkey.pfx", "password");

    // Create SAML Response object. 
    Response response = new MySamlResponse();

    // Create SAML Assertion object. 
    Assertion samlAssertion = new Assertion();
    Audience aud = new Audience();
    aud.Uri = "http://testuri.org";
    AudienceRestriction ar = new AudienceRestriction();
    ar.Audiences.Add(aud);

    // Set Conditions. 
    samlAssertion.Conditions = new Conditions();
    samlAssertion.Conditions.ConditionsList.Add(ar);

    // Sign the assertion. 
    samlAssertion.Sign(x509Certificate);

    // Add SAML Assertion 
    response.Assertions.Add(samlAssertion);

    // Convert the response object to XML. 
    XmlElement xmlElement = response.GetXml();

    System.Diagnostics.Trace.WriteLine(xmlElement.OuterXml);
    Console.WriteLine(xmlElement.OuterXml);
}

#endregion 
 
#region Using Event

static void AlterSamlAssertion_Event()
{
    X509Certificate2 x509Certificate = new X509Certificate2(@"..\..\Pkey.pfx", "password");

    // Create SAML Response object. 
    Response response = new Response();

    // Register the CreateElement to alter the elements. 
    SamlObject.CreateElement += response_CreateElement;

    Assertion samlAssertion = new Assertion();
    Audience aud = new Audience();
    aud.Uri = "http://testuri.org";
    AudienceRestriction ar = new AudienceRestriction();
    ar.Audiences.Add(aud);

    samlAssertion.Conditions = new Conditions();
    samlAssertion.Conditions.ConditionsList.Add(ar);

    // Sign the assertion. 
    samlAssertion.Sign(x509Certificate);

    // Add SAML Assertion 
    response.Assertions.Add(samlAssertion);

    // Convert the response object to XML. 
    XmlElement xmlElement = response.GetXml();

    System.Diagnostics.Trace.WriteLine(xmlElement.OuterXml);
    Console.WriteLine(xmlElement.OuterXml);

    // Unregister the CreateElement event handler. 
    SamlObject.CreateElement -= response_CreateElement;
}

static void response_CreateElement(object sender, CreateElementEventArgs e)
{
    // Change prefix of the element from "saml" to "saml2". 
    e.Prefix = e.Prefix.Replace("saml", "saml2");
}

#endregion

Framework

.NET Framework.NET Framework

Supported version: 2.0, 3.0, 3.5, 4.0, 4.5.x, 4.6.x and later
Assembly: ComponentPro.Saml (in ComponentPro.Saml.dll)

See Also