我正在按照本教程对 Azure App Config 进行 REST 调用,以使用 HMAC 身份验证添加键值条目。JS 和 c# 版本都不起作用 - 我最终得到一个 401 Unauthorized 错误说
HMAC-SHA256 error="invalid_token", error_description="无效签名"
我对 C# 实现的调用代码是:
var credential = "<my app config id>";
var secret = Encoding.ASCII.GetBytes(Base64Encode("<my app config secret>"));
UserQuery.MsgRouterConfigValue body = new MsgRouterConfigValue();
var key = "asd1";
body.clientId = Guid.NewGuid();
body.serviceUrl = new Uri("https://someuri");
using (var client = new HttpClient())
{
var request = new HttpRequestMessage()
{
RequestUri = new Uri($"<my app config url>/kv/{key}?label=dev&api-version=1.0"),
Method = HttpMethod.Put,
Content = new StringContent(JsonSerializer.Serialize(body))
};
Sign(request,credential, secret);
var resp = await client.SendAsync(request);
}
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
关于我用于凭证和机密的值,它们来自应用程序配置的门户:
谁能告诉我为什么会收到 401 错误?
编辑:所以这个错误归结为:
原因:提供的签名与服务器期望的不匹配。
解决方案:确保 String-To-Sign 正确。确保 Secret 正确且正确使用(使用前已解码 base64)。
因此,就“确保字符串签名正确”而言,我使用的代码与示例中提供的代码相同,这导致密码错误,但这与应用程序门户中定义的值相同配置。