使用托管标识对 Azure 服务总线进行身份验证的 REST API 调用

Authenticated REST API call to Azure Service Bus using Managed Identity

Azure 服务总线支持托管身份访问,但是我发现的唯一方法,例如,将消息发送到 queue 是使用这种需要代码和服务总线 SDK 的方法:

var tokenProvider = TokenProvider.CreateManagedServiceIdentityTokenProvider();
QueueClient sendClient = new QueueClient($"sb://{Config.Namespace}.servicebus.windows.net/", Config.Queue, tokenProvider);
await sendClient.SendAsync(new Message(Encoding.UTF8.GetBytes(messageInfo.MessageToSend)));
await sendClient.CloseAsync();

来源: https://github.com/Azure-Samples/app-service-msi-servicebus-dotnet https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-managed-service-identity

我正在寻找一种方法来通过 Azure API 管理策略中的 REST API 调用来执行相同的操作。我已经授予 APIM,基于角色的服务总线访问权限,并且我能够取回令牌,但是当我尝试使用托管的 REST API 调用时,我从服务总线返回了这个错误在授权中传递的身份令牌 header:

MalformedToken: The credentials contained in the WRAP header are not well-formed.

看起来服务总线目前可能只支持 WRAP 或 SAS 令牌及其 REST API:https://docs.microsoft.com/en-us/rest/api/servicebus/send-message-batch

但话又说回来,这在幕后是如何运作的?

TokenProvider.CreateManagedServiceIdentityTokenProvider()

似乎 REST 应该可以实现 API。

他们似乎在 SDK 中没有将其指定为 Authorization: Bearer tokenabcdef.....,而是指定为 Authorization: tokenabcdef。这有点不寻常。