【发布时间】:2021-10-27 19:33:29
【问题描述】:
我正在关注this 教程,该教程介绍了对 Azure App Config 进行 REST 调用以使用 HMAC 身份验证添加键值条目。 JS 和 c# 版本都不起作用 - 我最终得到 401 Unauthorized 错误提示
HMAC-SHA256 error="invalid_token", error_description="Invalid Signature"
我的 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 错误?
编辑: 所以这个错误归结为:
原因:提供的签名与服务器期望的不匹配。
解决方案:确保字符串签名正确。确保 Secret 正确且使用正确(使用前已解码 base64)。
因此,就“确保字符串签名正确”而言,我使用的代码与示例中提供的代码相同,这会导致密码错误,但这与门户中定义的值相同用于应用配置。
【问题讨论】:
标签: c# .net-core hmac azure-app-configuration