Fixed – The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters exception in Plugin (Dataverse / Dynamics 365)


Recently for one of our plugins, that moves attachments to the Azure Blob Storage, we got the below exception.

BlobFileName: WFU000069107_Inside Install – CFC__2024_03_03_12_24_05_179_temp.jpg
Caught Exception: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)


We were getting errors while trying to use the FromBase64ToString method to decode the Base64 encoded string into its original byte array.

Seemed there were some issues with the image uploaded, even from the browser when trying to download we were getting the below exception.

The solution was to check if it is a valid Base64 encoded string before converting it to a byte array and if not skip it and process other attachments in the plugin.

const string base64Pattern = @"^[A-Za-z0-9+/=]*$";

Hope it helps..

Advertisements