【问题标题】:How to decode metadata_storage_path produced by Azure Search indexer in .NET Core如何解码 .NET Core 中 Azure 搜索索引器生成的 metadata_storage_path
【发布时间】:2017-11-04 09:48:11
【问题描述】:

使用 .NetCore 1.1.2。

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

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

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."

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

【问题讨论】:

    标签: c# azure asp.net-core azure-cognitive-search azure-search-.net-sdk


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我在 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);
      }
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-02
          • 1970-01-01
          • 2019-02-05
          • 2018-10-28
          • 2019-06-19
          • 2018-11-19
          • 1970-01-01
          • 2020-12-23
          相关资源
          最近更新 更多