SAML Events
A number of callback delegates are provided that allow SAML protocol messages and SAML assertions to be accessed or modified during creation or processing.
For most applications, using these delegates is optional.
ISamlServiceProviderEvents
The ISamlServiceProviderEvents interface provides access to SAML SSO and SLO events when acting as the service provider.
ISamlServiceProviderEvents extends the ISamlProviderEvents interface.
OnAuthnRequestCreated
OnAuthnRequestCreated is called when a SAML authentication request has been created.
OnSamlResponseReceived
OnSamlResponseReceived is called when a SAML response is received from an identity provider.
OnSamlAssertionReceived
OnSamlAssertionReceived is called when a SAML assertion is received from an identity provider.
ISamlIdentityProviderEvents
The ISamlIdentityProviderEvents interface provides access to SAML SSO and SLO events when acting as the identity provider.
ISamlIdentityProviderEvents extends the ISamlProviderEvents interface.
OnAuthnRequestReceived
OnAuthnRequestReceived is called when a SAML authentication request is received from a service provider.
OnSamlAssertionCreated
OnSamlAssertionCreated is called when a SAML assertion has been created.
OnSamlResponseCreated
OnSamlResponseCreated is called when a SAML response has been created.
ISamlProviderEvents
The ISamlProviderEvents interface is the base interface for ISamlServiceProviderEvents and ISamlIdentityProviderEvents.
OnResolveUrl
OnResolveUrl is called to resolve the destination URL when sending a SAML message.
OnSendMessage
OnSendMessage is called when sending a SAML message.
OnReceiveMessage
OnReceiveMessage is called when receiving a SAML message.
OnGenerateSignature
OnGenerateSignature is called when a signature has been generated.
OnVerifySignature
OnVerifySignature is called when a signature has been verified.
OnEncrypt
OnDecrypt
OnDecrypt is called when data has been decrypted.
OnLogoutRequestCreated
OnLogoutRequestCreated is called when a SAML logout request has been created.
OnLogoutResponseCreated
OnLogoutResponseCreated is called when a SAML logout response has been created.
OnLogoutRequestReceived
OnLogoutRequestReceived is called when a SAML logout request is received.
OnLogoutResponseReceived
OnLogoutResponseReceived is called when a SAML logout response is received.
OnArtifactResolveCreated
OnArtifactResolveCreated is called when a SAML artifact resolve request is created.
OnArtifactResponseCreated
OnArtifactResponseCreated is called when a SAML artifact response is created.
OnArtifactResolveReceived
OnArtifactResolveReceived is called when a SAML artifact resolve request is received.
OnArtifactResponseReceived
OnArtifactResponseReceived is called when a SAML artifact response is received.
Event Examples
Adding a Query String
The following example demonstrates adding a query string to the single sign-on service URL before sending the SAML authn request to the IdP.
_samlServiceProvider.Events.OnResolveUrl += (httpContext, samlEndpointType, url) =>
{
return QueryHelpers.AddQueryString(url, "username", "[email protected]");
};
await _samlServiceProvider.InitiateSsoAsync(partnerName, returnUrl);
Adding Advice to the SAML Assertion
The following example demonstrates adding advice to the SAML assertion.
// Add optional advice to the SAML assertion.
_samlIdentityProvider.Events.OnSamlAssertionCreated += (httpContext, samlAssertion) =>
{
samlAssertion.Advice = new Advice()
{
AdviceList = new List<AdviceListItem>()
{
new AdviceListItem()
{
SamlAssertion = new SamlAssertion()
{
Issuer = samlAssertion.Issuer,
Subject = new Subject()
{
NameID = new NameID()
{
Name = "[email protected]"
}
}
}
}
}
};
return samlAssertion;
};
return _samlIdentityProvider.SendSsoAsync(userName, attributes);
Adding SAML Extensions
The following example demonstrates adding SAML extensions to a SAML authentication request.
// Include SAML extensions in the authn request.
_samlServiceProvider.Events.OnAuthnRequestCreated += (httpContext, authnRequest) =>
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<test xmlns=\"urn:test\">This is a test</test>");
authnRequest.Extensions = new Extensions()
{
Items = new List<XmlElement>() { xmlDocument.DocumentElement }
};
return authnRequest;
};
await _samlServiceProvider.InitiateSsoAsync(partnerName);
Retrieving SAML Extensions
The following example demonstrates receiving SAML extensions in a SAML authentication request.
// Receive the SAML extensions included in the authn request.
IEnumerable<XmlElement> extensionsItems = null;
_samlIdentityProvider.Events.OnAuthnRequestReceived += (httpContext, authnRequest, relayState) =>
{
extensionsItems = authnRequest.Extensions?.Items;
};
await _samlIdentityProvider.ReceiveSsoAsync();