Back to Blog
Mar 25, 2023

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

Written by Zack Schwartz

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.



picture of the author
Zack Schwartz @apexdodge

ENTREPRENEUR & SOFTWARE ENGINEER, AUSTIN, TX
I enjoy tackling a wide array of business challenges ranging from front line product support and operations to sales and marketing efforts. My core expertise is in software development building enterprise level web applications.


Subscribe to the Raytha newsletter