【发布时间】:2019-11-04 22:05:13
【问题描述】:
我正在尝试通过 ARM 模板部署 APIM 策略文件并收到以下错误:
第 24 行第 6 列的元素 'set-variable' 出错:代码块缺少结束 \\"}\\" 字符。确保此块中的所有 \\"{\\" 字符都有一个匹配的 \\"}\\" 字符,并且没有任何 \\"}\\" 字符被解释为标记。 em>
我最初通过 Azure 门户中的 APIM 管理刀片创建了此策略,相关策略在此处如下所示:
<set-variable name="digitalSignature" value="@{
string privateKey = context.Variables.GetValueOrDefault<string>("privateKey", "");
Encoding encoding = System.Text.Encoding.ASCII;
string usablePrivateKey = privateKey.Replace("-", "+").Replace("_", "/");
byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
byte[] encodedPathAndQueryBytes = encoding.GetBytes(context.Request.Url.Path + context.Request.Url.QueryString);
HMACSHA1 hashAlgorithm = new HMACSHA1(privateKeyBytes);
byte[] hash = hashAlgorithm.ComputeHash(encodedPathAndQueryBytes);
string digitalSignature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
return digitalSignature;
}" />
但是,该表达式包含许多不是有效 XML 的字符,因此我在 *.policy.xml 文件中将上述代码转义如下:
<set-variable name="digitalSignature" value="@{
string privateKey = context.Variables.GetValueOrDefault<string>("privateKey", "");
Encoding encoding = System.Text.Encoding.ASCII;
string usablePrivateKey = privateKey.Replace("-", "+").Replace("_", "/");
byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
byte[] encodedPathAndQueryBytes = encoding.GetBytes(context.Request.Url.Path + context.Request.Url.QueryString);
HMACSHA1 hashAlgorithm = new HMACSHA1(privateKeyBytes);
byte[] hash = hashAlgorithm.ComputeHash(encodedPathAndQueryBytes);
string digitalSignature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
return digitalSignature;
}" />
我错过了什么?由于支持 APIM 实例的 GIT 存储库中的策略 XML 文件没有转义,我什至需要转义字符吗?
【问题讨论】: