The Rock Solid Knowledge SAML component serializes SAML messages to Xml by default.
it may be necessary in certain cases to either completely replace or extend this serialization to, for example take in to account identity providers that do not follow the SAML specification.
Extending the Xml serialization
To extend the way SAML messages are serialized, you will need to derive from the SamlMessageXmlSerializer class and override the Serialize method.
In your implementation of serialize, you would typically call the base classes Serialize method to convert the passed in SamlMessage to an XmlElement and then modify this element as per your requirements before returning the modified element.
Code like the following can be used as a starting point for your implementation:
public class MySamlMessageXmlSerializer : SamlMessageXmlSerializer {
public XmlElement Serialize(SamlMessage message)
{
var serializedMessage =base.Serialized(message);
//edit the xml element as required here.
return serializedMessage;
}
}
Completely replacing the default mechanism for serializing Saml messages
Whilst it is not recommended, we do provide the capability to completely replace the way we serialize SamlMessages to Xml.
If you need to do this, you can create an implementation of the ISamlMessageXmlSerializer interface.
This interface has a single method "Serialize" which accepts a SamlMessage and returns an XmlElement.
a potential implementation could look as follows:
public class MySamlMessageXmlSerializer : ISamlMessageXmlSerializer
{
public XElement Serialize(SamlMessage message)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
}
Registering your new implementation with dependency injection
Once you have either extended or replaced the way SamlMessages are serialized, you will need to register your implementation with dependency injection.
code like the following can be used to do this:
services.AddTransient<ISamlMessageXmlMessageSerializer, MyXmlMessageSerializer>();