Fernando Silva
Fernando Silva

Reputation: 364

Azure Search invalid base64 path in metadata using Blob Storage Indexer

I used Blob Storage Indexer to index my files in Azure Search.

After performing a search, I was supposed to be able to determine the path of blob by decoding a base64 metadata string. From what I can tell, C# is unable to decode the returned base64.

Here are sample base64 paths sent from Azure Search:

1. aHR0cHM6Ly9qbG9jYWxnZW5lcmFsLmJsb2IuY29yZS53aW5kb3dzLm5ldC9kaWFyaW9zLzEvMTAtdGV4dG8uemlw0 -> When I try to decode it in C#, I get the following exception: "Invalid length for a Base-64 char array or string." Code used is bellow

2.
aHR0cHM6Ly9qbG9jYWxnZW5lcmFsLmJsb2IuY29yZS53aW5kb3dzLm5ldC9kaWFyaW9zLzEvMi10ZXh0by56aXA1 -> adds a 5 to the end, that is not in the original path

3. aHR0cHM6Ly9qbG9jYWxnZW5lcmFsLmJsb2IuY29yZS53aW5kb3dzLm5ldC9kaWFyaW9zLzEvMy10ZXh0by56aXA1 -> adds a 5 to the end, that is not in the original path

Here is the code I am using to decode it:

    public static string DecodeBase64(this string base64)
    {
        // I have also tried with ASCII
        return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(base64));
    }

enter image description here

Upvotes: 1

Views: 1038

Answers (2)

Justin Ross
Justin Ross

Reputation: 21

private string DecodeBase64String(string encodedString)
{
    var encodedStringWithoutTrailingCharacter = encodedString.Substring(0, encodedString.Length - 1);
    var encodedBytes = WebEncoders.Base64UrlDecode(encodedStringWithoutTrailingCharacter);
    return HttpUtility.UrlDecode(encodedBytes, Encoding.UTF8);
}

Upvotes: 0

Eugene Shvets
Eugene Shvets

Reputation: 4671

Please refer to the documenation. Specifically, you need to use UrlTokenDecode method or an equivalent.

Upvotes: 3

Related Questions