如何解码.NET Core中Azure搜索索引器生成的metadata_storage_path

问题描述 投票:3回答:3

使用.Net Core 1.1.2。

通过Azure Search SDK成功获取搜索结果后,我正在尝试解码metadata_storage_path值。我见过有人说要在.NET中使用HttpServerUtility.UrlTokenDecode或在其他语言中使用等效的as seen here

那么问题就变成了,HttpServerUtility.UrlTokenDecode的.NetCore中的等价物是什么?附:

var pathEncoded = "aHR0cHM6Ly9mYWtlZC5ibG9iLmNvcmUud2luZG93cy5uZXQvcGRmYmxvYnMvYW5udWFsX3JlcG9ydF8yMDA5XzI0NTU20";

我尝试过以下方法:

var pathbytes = Convert.FromBase64String(pathEncoded); 
//Throws System.FormatException "Invalid length for a Base-64 char array or string."

var pathbytes = WebEncoders.Base64UrlDecode(pathEncoded);
//Throws System.FormatException - "TODO: Malformed input."

有趣的是,如果我切断路径中的最后一个字符,一切正常。编码...用Microsoft.AspNetCore 1.1.2处理这种情况的正确方法是什么?

c# azure asp.net-core azure-search azure-search-.net-sdk
3个回答
5
投票

HttpServerUtility.UrlTokenEncode在编码字符串中附加一个额外的尾随字符。你做得对 - 只需删除那个额外的角色并使用WebEncoders.Base64UrlDecode。有关详细信息,请参阅this Q&A


3
投票

我在asp.net core 2.1中使用了以下函数来编码Azure搜索中的meta_storage_path值。

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

1
投票

我只想补充一点,您还可以取消选择“Base-64 Encode Keys”Azure搜索索引器选项。

注意:仅对没有字符的字段执行此操作Azure认为文档密钥无效。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.