When developing a claim-based authentication application, in order to make the application working when deployed to load balancing servers, I followed http://msdn.microsoft.com/en-us/library/ff803371.aspx to replace the default SecurityTokenHandler by using a RsaTokenTransform handler to encrypt the session cookies using a self-provided certificate:
X509Certificate2 serviceCertificate = new X509Certificate2(certificate, certificatePassword);
List<CookieTransform> sessionTransformers =
new List<CookieTransform>
(
new CookieTransform[]
{
new DeflateCookieTransform(),
new RsaEncryptionCookieTransform(serviceCertificate),
new RsaSignatureCookieTransform(serviceCertificate)
}
);
SessionSecurityTokenHandler sessionHandler = new SessionSecurityTokenHandler(sessionTransformers.AsReadOnly());
FederatedAuthentication.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
To make the application more flexible and portable, the certificate is pulled from a database and stored in a X509Certificate2 object at runtime.
This is working with one server hosting the application but once I switched to a server farm, every now and then I got the error:
ID1014: The signature is not valid. The data may have been tampered with
Also some javascript, css and font files failed to load because of the same issue.
Related questions but did not solve the issue:
WIF- ID1014: The signature is not valid. The data may have been tampered with
WIF: ID1014: The signature is not valid. The data may have been tampered with