Gets or sets the list of attribute.
public IList<Attribute> Attributes { get; set; }
Public Property Attributes As IList(Of Attribute)
public:
property IList<Attribute> Attributes {
IList<Attribute> get();
void set(IList<Attribute> Attributes);
}
Shows how to create, sign and validate an Assertion object.
using System;
using System.Security.Cryptography.X509Certificates;
using ComponentPro.Saml;
using ComponentPro.Saml1;
...
static Assertion CreateAssertion()
{
// Create a new instance of the Assertion class.
Assertion assertion = new Assertion();
// Set Issuer
assertion.Issuer = "urn:test";
// Set Conditions
assertion.Conditions = new Conditions(new TimeSpan(1, 0, 0));
// Create an AuthenticationStatement.
AuthenticationStatement authenticationStatement = new AuthenticationStatement(AuthenticationMethodIdenfifiers.Password);
NameIdentifier nameIdentifier = new NameIdentifier("urn:test", NameIdentifierFormats.X509SubjectName, "uid=test,ou=People,dc=test,dc=com");
SubjectConfirmation subjectConfirmation = new SubjectConfirmation(ConfirmationMethods.Bearer);
authenticationStatement.Subject = new Subject(nameIdentifier, subjectConfirmation);
// Add the AuthenticationStatement to the Assertion.
assertion.Statements.Add(authenticationStatement);
// Add attributes to the Assertion.
AttributeStatement attributeStatement = new AttributeStatement();
attributeStatement.Subject = authenticationStatement.Subject;
attributeStatement.Attributes.Add(new ComponentPro.Saml1.Attribute("email", "urn:test", "john@test.com"));
attributeStatement.Attributes.Add(new ComponentPro.Saml1.Attribute("FirstName", "urn:test", "John"));
attributeStatement.Attributes.Add(new ComponentPro.Saml1.Attribute("LastName", "urn:test", "Vu"));
assertion.Statements.Add(attributeStatement);
return assertion;
}
static void SignAssertion(Assertion assertion)
{
// Load certificate to sign the assertion.
X509Certificate2 cert = new X509Certificate2("Pkey.pfx", "password");
// Sign the assertion
assertion.Sign(cert);
}
static void ValidateAssertion(Assertion assertion)
{
if (assertion.IsSigned())
{
bool result = assertion.Validate();
if (result)
System.Diagnostics.Trace.WriteLine("Assertion is validated");
else
System.Diagnostics.Trace.WriteLine("Assertion cannot be validated");
}
}
static void Main()
{
try
{
Assertion assertion = CreateAssertion();
SignAssertion(assertion);
ValidateAssertion(assertion);
}
catch (SamlException samlException)
{
// Handle the exception here.
// ...
}
}
Imports System.Security.Cryptography.X509Certificates
Imports ComponentPro.Saml
Imports ComponentPro.Saml1
...
Private Shared Function CreateAssertion() As Assertion
' Create a new instance of the Assertion class.
Dim assertion As New Assertion()
' Set Issuer
assertion.Issuer = "urn:test"
' Set Conditions
assertion.Conditions = New Conditions(New TimeSpan(1, 0, 0))
' Create an AuthenticationStatement.
Dim authenticationStatement As New AuthenticationStatement(AuthenticationMethodIdenfifiers.Password)
Dim nameIdentifier As New NameIdentifier("urn:test", NameIdentifierFormats.X509SubjectName, "uid=test,ou=People,dc=test,dc=com")
Dim subjectConfirmation As New SubjectConfirmation(ConfirmationMethods.Bearer)
authenticationStatement.Subject = New Subject(nameIdentifier, subjectConfirmation)
' Add the AuthenticationStatement to the Assertion.
assertion.Statements.Add(authenticationStatement)
' Add attributes to the Assertion.
Dim attributeStatement As New AttributeStatement()
attributeStatement.Subject = authenticationStatement.Subject
attributeStatement.Attributes.Add(New ComponentPro.Saml1.Attribute("email", "urn:test", "john@test.com"))
attributeStatement.Attributes.Add(New ComponentPro.Saml1.Attribute("FirstName", "urn:test", "John"))
attributeStatement.Attributes.Add(New ComponentPro.Saml1.Attribute("LastName", "urn:test", "Vu"))
assertion.Statements.Add(attributeStatement)
Return assertion
End Function
Private Shared Sub SignAssertion(ByVal assertion As Assertion)
' Load certificate to sign the assertion.
Dim cert As New X509Certificate2("Pkey.pfx", "password")
' Sign the assertion
assertion.Sign(cert)
End Sub
Private Shared Sub ValidateAssertion(ByVal assertion As Assertion)
If assertion.IsSigned() Then
Dim result As Boolean = assertion.Validate()
If result Then
System.Diagnostics.Trace.WriteLine("Assertion is validated")
Else
System.Diagnostics.Trace.WriteLine("Assertion cannot be validated")
End If
End If
End Sub
Shared Sub Main()
Try
Dim assertion As Assertion = CreateAssertion()
SignAssertion(assertion)
ValidateAssertion(assertion)
Catch samlException As SamlException
' Handle the exception here.
' ...
End Try
End Sub