简体   繁体   English

Azure.Messaging.ServiceBus 使用系统分配的托管标识创建 ServiceBusClient

[英]Azure.Messaging.ServiceBus Create a ServiceBusClient using a System Assigned Managed Identity

I'm migrating a servicebus client application from Microsoft.Azure.ServiceBus to use the current library Azure.Messaging.ServiceBus.我正在从 Microsoft.Azure.ServiceBus 迁移服务总线客户端应用程序以使用当前库 Azure.Messaging.ServiceBus。

The application is a Worker Process running on a virtual machine in windows azure.该应用程序是运行在windows azure虚拟机上的一个Worker Process。

The VM has a system assigned managed identity which grants it access to service bus and we have been using it successfully with the old library for over a year. VM 有一个系统分配的托管身份,授予它访问服务总线的权限,我们已经成功地将它与旧库一起使用了一年多。

On the old library we created a client using this connection string在旧库上,我们使用此连接字符串创建了一个客户端

Endpoint=sb://MyNamespace.servicebus.windows.net/;Authentication=Managed Identity

When I put that connection string into the constructor of Azure.Messaging.ServiceBus.ServiceBusClient I get the following error当我将该连接字符串放入 Azure.Messaging.ServiceBus.ServiceBusClient 的构造函数时,出现以下错误

The connection string used for an Service Bus client must specify the Service Bus namespace host and either a Shared Access Key (both the name and value) OR a Shared Access Signature to be valid. (Parameter 'connectionString')

I've been trawling through documents for some time now with no progress.我一直在浏览文件一段时间,但没有任何进展。 Is there anyway to make this work?有没有办法使这项工作?

Ideally I would continue to use the connection string - developer machines do not have system assigned ID's so we develop with key based connection strings and let devops swap in the correct prod connection string.理想情况下,我会继续使用连接字符串——开发人员机器没有系统分配的 ID,因此我们使用基于密钥的连接字符串进行开发,并让 devops 交换正确的产品连接字符串。

UPDATE更新

Following on from Jesse's answer managed identity has to go trough a separate constructor which requires a namespace instead of an endpoint and an instance of ManagedIdentityCredential.在 Jesse 的回答之后,托管身份必须通过一个单独的构造函数达到 go,该构造函数需要一个命名空间而不是端点和 ManagedIdentityCredential 的一个实例。

As I mentioned not all environments where we deploy have managed aged identities, some require a SharedAccessKey based connection string.正如我提到的,并非我们部署的所有环境都管理过时的身份,有些环境需要基于 SharedAccessKey 的连接字符串。

Instead introducing new "identity type" configuration parameters into our build process I've used a factory method to parse the connection string and call the correct constructor overload.我使用工厂方法来解析连接字符串并调用正确的构造函数重载,而不是在我们的构建过程中引入新的“身份类型”配置参数。 Where its a managed identity It extracts the namespace from the endpoint setting.它是一个托管身份,它从端点设置中提取命名空间。

I Hope its useful for others我希望它对其他人有用

        private static ServiceBusClient CreateServiceBusClient(string connectionString)
        {
            var cs = new DbConnectionStringBuilder();
            cs.ConnectionString = connectionString;
            if (cs.ContainsKey("Authentication") &&
                "Managed Identity".Equals(cs["Authentication"].ToString(), StringComparison.OrdinalIgnoreCase))
            {
                string endpoint = cs["Endpoint"].ToString() ?? String.Empty;
                if (endpoint.StartsWith(@"sb://", StringComparison.OrdinalIgnoreCase)) endpoint = endpoint.Substring(5);
                if (endpoint.EndsWith(@"/")) endpoint = endpoint.Substring(0, endpoint.Length - 1);
                return new ServiceBusClient(endpoint, new ManagedIdentityCredential());
            }

            return new ServiceBusClient(connectionString);
        }

it needs the Azure.Identity package and the namespace System.Data.Common for the connection string builder.它需要 Azure.Identity package 和连接字符串生成器的命名空间 System.Data.Common。

The clients in the Azure.Messaging.ServiceBus package support connection strings only in the format that the Azure portal returns them. Azure.Messaging.ServiceBus package 中的客户端仅支持 Azure 门户返回的格式的连接字符串。 The ;Authentication=Managed Identity token that you've included in your connection string is not a known token and is ignored, so the client does not have the information needed to perform authorization.您在连接字符串中包含的;Authentication=Managed Identity令牌不是已知令牌并被忽略,因此客户端没有执行授权所需的信息。 A managed identity cannot be specified via connection string.无法通过连接字符串指定托管标识。

To use a managed identity, you'll use one of the constructor overloads that accepts a fully qualified namespace and a TokenCredential .要使用托管标识,您将使用接受完全限定命名空间和TokenCredential的构造函数重载之一。 An example can be found in the package Overview docs.可以在 package 概述文档中找到一个示例。 Any of the Azure.Identity credentials can be used;可以使用Azure.Identity凭证中的任何一个; you may want to take take a look at the managed identity section of the Azure.Identity overview.您可能想看看Azure.Identity概述的托管身份部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure.Messaging.ServiceBus QueueProperties 属性 DefaultMessageTimeToLive 未正确更新 - Azure.Messaging.ServiceBus QueueProperties property DefaultMessageTimeToLive not updating correctly .Net5 Azure Function | Azure.Messaging.ServiceBus - .Net5 Azure Function | Azure.Messaging.ServiceBus 使用新的 Azure.messaging.servicebus 获取活动消息数 - Get active message count with the new Azure.messaging.servicebus Azure PHP web 应用程序使用系统分配的托管身份连接到 Azure 存储 Blob - Azure PHP web app using system assigned managed identity connecting to Azure Storage Blob 如何在运行中为非常长的处理消息更新锁定 - Azure.Messaging.ServiceBus; - how to renew lock for a very long processing message on the fly - Azure.Messaging.ServiceBus; 无法使用系统分配的托管标识 ID 登录到 Azure - Cannot login to Azure with system assigned managed identity ID 无法将带有系统分配托管标识的 Azure 逻辑应用程序连接到 Azure SQL 服务器 - Unable to connect Azure Logic App w/ System Assigned Managed Identity to Azure SQL Server 将托管标识与 Azure 服务总线一起使用 - Using a managed identity with Azure Service Bus 使用托管标识从 Azure 应用服务调用图 - Call Graph from Azure App Service using Managed Identity Azure - 使用托管标识向 KeyVault 和其他资源验证 AKS - Azure - Using a Managed Identity to authenticate AKS to KeyVault and other resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM