Represents the SAML Response element.
public class Response : StatusResponseType
Public Class Response
Inherits StatusResponseType
public ref class Response : public StatusResponseType
Remarks
The Response message element is used when a response consists of a list of zero or more assertions
that satisfy the request.
Shows how to create a Response object and sign it.
using System;
using ComponentPro.Saml2;
using System.Xml;
using System.Security.Cryptography.X509Certificates;
...
X509Certificate2 x509Certificate = new X509Certificate2(@"..\..\Pkey.pfx", "password");
// Create SAML Response object.
Response response = new Response();
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);
samlAssertion.Sign(x509Certificate);
// Add SAML Assertion
response.Assertions.Add(samlAssertion);
// You can also sign the SAML response with the following code
response.Sign(x509Certificate);
XmlElement xmlElement = response.GetXml();
System.Diagnostics.Trace.WriteLine(xmlElement.OuterXml);
Console.WriteLine(xmlElement.OuterXml);
Imports ComponentPro.Saml2
Imports System.Xml
Imports System.Security.Cryptography.X509Certificates
...
Dim x509Certificate As New X509Certificate2("..\..\Pkey.pfx", "password")
' Create SAML Response object.
Dim response As New Response()
Dim samlAssertion As New Assertion()
Dim aud As New Audience()
aud.Uri = "http://testuri.org"
Dim ar As New AudienceRestriction()
ar.Audiences.Add(aud)
samlAssertion.Conditions = New Conditions()
samlAssertion.Conditions.ConditionsList.Add(ar)
samlAssertion.Sign(x509Certificate)
' Add SAML Assertion
response.Assertions.Add(samlAssertion)
' You can also sign the SAML response with the following code
response.Sign(x509Certificate)
Dim xmlElement As XmlElement = response.GetXml()
System.Diagnostics.Trace.WriteLine(xmlElement.OuterXml)
Console.WriteLine(xmlElement.OuterXml)
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
Imports ComponentPro.Saml2
Imports ComponentPro.Saml
Imports System.Xml
Imports System.Security.Cryptography.X509Certificates
...
#Region "Using A Custom Class"
Private Class MySamlResponse
Inherits Response
Public Overrides Function GetXml(ByVal xmlDocument As XmlDocument) As XmlElement
Dim xml As XmlElement = MyBase.GetXml(xmlDocument)
' Alter the xml here;
' Finding the element 'InclusiveNamespaces' by using the Select method.
Dim inclusiveNamespacesNode As XmlNode = xml.SelectSingleNode("xpath to your InclusiveNamespaces")
xml.RemoveChild(inclusiveNamespacesNode)
Dim keyInfoNode As XmlNode = xml.SelectSingleNode("xpath to your KeyInfo node")
' Your node manipulation here.
' ...
Return xml
End Function
End Class
Private Shared Sub AlterSamlAssertion_CustomClass()
' Load the private key to sign the assertion.
Dim x509Certificate As New X509Certificate2("..\..\Pkey.pfx", "password")
' Create SAML Response object.
Dim response As Response = New MySamlResponse()
' Create SAML Assertion object.
Dim samlAssertion As New Assertion()
Dim aud As New Audience()
aud.Uri = "http://testuri.org"
Dim ar As 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.
Dim xmlElement As XmlElement = response.GetXml()
System.Diagnostics.Trace.WriteLine(xmlElement.OuterXml)
Console.WriteLine(xmlElement.OuterXml)
End Sub
#End Region
#Region "Using Event"
Private Shared Sub AlterSamlAssertion_Event()
Dim x509Certificate As New X509Certificate2("..\..\Pkey.pfx", "password")
' Create SAML Response object.
Dim response As New Response()
' Register the CreateElement to alter the elements.
AddHandler SamlObject.CreateElement, AddressOf response_CreateElement
Dim samlAssertion As New Assertion()
Dim aud As New Audience()
aud.Uri = "http://testuri.org"
Dim ar As 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.
Dim xmlElement As XmlElement = response.GetXml()
System.Diagnostics.Trace.WriteLine(xmlElement.OuterXml)
Console.WriteLine(xmlElement.OuterXml)
' Unregister the CreateElement event handler.
RemoveHandler SamlObject.CreateElement, AddressOf response_CreateElement
End Sub
Private Shared Sub response_CreateElement(ByVal sender As Object, ByVal e As CreateElementEventArgs)
' Change prefix of the element from "saml" to "saml2".
e.Prefix = e.Prefix.Replace("saml", "saml2")
End Sub
#End Region
ComponentPro.Saml2.ComponentPro.Saml2.Response