Resolving SAML error: Unexpected error occurred: ASN1 corrupted data.

Resolving SAML error: Unexpected error occurred: ASN1 corrupted data.
Recently, I had an infuriating experience implementing SAML in .NET 6+ while trying to load an x509 Certificate via the string method. We store the x509 cert in the database instead of a file on the file system, so we wanted to load the string value.

However, we kept getting an error when loading the certificate:

Unexpected error occurred: ASN1 corrupted data.

What made this even more frustrating was the fact that it was working on our local developer Windows machines but failing in production, which ran on Linux Docker containers.

We narrowed it down to a theory that Windows and Linux differ in how they retrieve bytes, as demonstrated with this line of code:

The problem was that we were using Encoding.ASCII.GetBytes(cert) instead of Convert.FromBase64String(cert). When we made this change, it worked.


Share this post