{"record":{"id":"5d85d45549a922fd","repo":"OrchardCMS/OrchardCore","slug":"invalid-base64-string","errorCode":null,"errorMessage":"Invalid Base64 string.","messagePattern":"Invalid Base64 string\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Base64.cs","lineNumber":25,"sourceCode":"    /// <summary>\n    /// Converts a base64 encoded UTF8 string to the original value.\n    /// </summary>\n    /// <param name=\"base64\">The base64 encoded string.</param>\n    /// <returns>The decoded string.</returns>\n    /// <remarks>This method is equivalent to <c>Encoding.UTF8.GetString(Convert.FromBase64String(base64))</c> but uses a buffer pool to decode the string.</remarks>\n    public static string FromUTF8Base64String(string base64)\n    {\n        ArgumentNullException.ThrowIfNull(base64);\n\n        // Due to padding the deserialized buffer could be smaller than this value.\n        var maxBufferLength = GetDeserializedBase64Length(base64.Length);\n\n        using var memoryStream = MemoryStreamFactory.GetStream(maxBufferLength);\n        var span = memoryStream.GetSpan(maxBufferLength);\n\n        if (!Convert.TryFromBase64String(base64, span, out var bytesWritten))\n        {\n            throw new FormatException(\"Invalid Base64 string.\");\n        }\n\n        return Encoding.UTF8.GetString(span.Slice(0, bytesWritten));\n    }\n\n    [Obsolete(\"This will be deprecated in v4. Please use DecodeToStream instead.\")]\n    public static Stream DecodedToStream(string base64)\n        => DecodeToStream(base64);\n\n    /// <summary>\n    /// Converts a base64 encoded string to a stream.\n    /// </summary>\n    /// <param name=\"base64\">The base64 encoded string.</param>\n    /// <remarks>The resulting <see cref=\"Stream\"/> is positioned at index 0 and should be disposed once used.</remarks>\n    /// <returns>The decoded stream.</returns>\n    /// <exception cref=\"FormatException\"></exception>\n    public static Stream DecodeToStream(string base64)\n    {","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Base64.cs#L7-L43","documentation":"Base64.FromUTF8Base64String decodes a Base64-encoded UTF-8 string in-place and throws FormatException when Convert.TryFromBase64String fails, i.e. the input contains characters or padding that are not valid Base64.","triggerScenarios":"Passing a string with characters outside the Base64 alphabet (A–Z, a–z, 0–9, +, /), wrong length (not a multiple of 4), or truncated/misplaced padding to FromUTF8Base64String.","commonSituations":"Storing an already-decoded string instead of the encoded one; URL-safe tokens edited/truncated by copy-paste; data corrupted by a form/JSON round-trip.","solutions":["Validate the input matches the Base64 pattern before calling (see validationCode).","Determine whether the sender used Base64Url encoding (- and _ instead of + and /) and normalize accordingly.","Fix the producer of the string so a properly encoded Base64 value is stored/transmitted.","Catch FormatException and surface a user-friendly 'invalid encoded value' message."],"exampleFix":"// before\nvar text = Base64.FromUTF8Base64String(input); // throws on url-safe base64\n// after\nvar normalized = input.Replace('-', '+').Replace('_', '/');\nvar text = Base64.FromUTF8Base64String(normalized);","handlingStrategy":"validation","validationCode":"private static readonly System.Text.RegularExpressions.Regex Base64Rx =\n    new(\"^[A-Za-z0-9+/]*={0,2}$\", System.Text.RegularExpressions.RegexOptions.Compiled);\n\nbool IsValidBase64(string s) =>\n    !string.IsNullOrEmpty(s) && s.Length % 4 == 0 && Base64Rx.IsMatch(s);","typeGuard":"static bool IsBase64String([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? s) =>\n    !string.IsNullOrEmpty(s) && s.Length % 4 == 0 &&\n    Convert.TryFromBase64String(s, new byte[s.Length], out _);","tryCatchPattern":"try\n{\n    var text = Base64.FromUTF8Base64String(input);\n}\ncatch (FormatException)\n{\n    throw new ArgumentException(\"The provided value is not a valid Base64 string.\", nameof(input));\n}","preventionTips":["Validate Base64 (alphabet, length % 4, padding) at input boundaries.","Detect Base64Url input (-/_) and normalize to standard Base64 before decoding.","Ensure producers store the encoded form, not already-decoded text."],"tags":["base64","decoding","format"],"backgroundTag":"invalid-argument-format","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}