Skip to content

Instantly share code, notes, and snippets.

@jpda
Last active March 4, 2019 19:43
Show Gist options
  • Save jpda/689e586e9a1740bd5f8cbb2e9c0a46a4 to your computer and use it in GitHub Desktop.
Save jpda/689e586e9a1740bd5f8cbb2e9c0a46a4 to your computer and use it in GitHub Desktop.
Decoding Azure Search document keys in .net core without asp.net
public async Task Go()
{
var response = await _client.GetStringAsync("https://<SEARCH_SERVICE>.search.windows.net/indexes/<INDEX>/docs?api-version=2017-11-11&search=*&%24top=100&%24select=metadata_storage_path");
dynamic data = JsonConvert.DeserializeObject(response);
foreach (dynamic a in data["value"])
{
string originalPath = a.metadata_storage_path.ToString();
// remove extra char
string path = originalPath.Substring(0, originalPath.Length - 1);
// fix padding - remove extra characters that the .netfx UrlTokenEncode adds
// see https://docs.microsoft.com/en-us/azure/search/search-indexer-field-mappings#base64decode
var mod = path.Length % 4;
var mod2 = mod % 2;
path = path.PadRight(path.Length + (mod > 2 ? m2 : mod), '=');
Console.WriteLine($"{path}: {System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(path))}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment