Спасибо, Томас.Это сработало, когда я его реализовал, ссылаясь на токен JWT .
Я удалился, используя TryParseBasic
вместо AsBasic
.
<policies>
<inbound>
<set-backend-service id="apim-generated-policy" backend-id="preaddresscode2" />
<rewrite-uri template="/HttpTrigger1" />
<set-variable name="isAuthOk" value="@{
string[] value;
if (context.Request.Headers.TryGetValue("Authorization", out value))
{
BasicAuthCredentials credit = context.Request.Headers.GetValueOrDefault("Authorization","").AsBasic();
if(credit == null){
return false;
}
switch(credit.UserId){
case "UUUUUU1":
// it seems an ugly implementation.
if(credit.Password.Equals("PPPPPP1")){
return true;
}
case "UUUUUU2":
if(credit.Password.Equals("PPPPPP2")){
return true;
}
break;
default:
break;
}
return false;
}
else
{
return false;
}
return true;
}" />
<base />
<choose>
<when condition="@(context.Variables.GetValueOrDefault<bool>("isAuthOk"))" />
<otherwise>
<return-response>
<set-status code="401" reason="Unauthorized" />
<set-header name="WWW-Authenticate" exists-action="override">
<value>Basic realm="someRealm"</value>
</set-header>
<set-body>Wrong username or password</set-body>
</return-response>
</otherwise>
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>