azure - Azure.Messaging.ServiceBus 命名空间中是否有 CreateSharedAccessSignatureTokenProvider 的替代品?

标签 azure azureservicebus azure-eventhub

我有为 Azure 服务总线(和事件中心)的分布式客户端生成 token 的代码。这与旧的 Nuget 包 Microsoft.Azure.ServiceBus 和 Microsoft.Azure.EventHubs 配合得很好,这些包似乎已被弃用。

我已升级其余代码以使用 Azure.Messaging.ServiceBus 和 Azure.Messaging.EventHubs,但我无法找到 API 的替代品来生成要在远程应用程序上使用的 token CreateSharedAccessSignatureTokenProvider

我认为答案是相同的,但在替换 EventHubs 后我需要做同样的事情。

是否有 Azure EventHubs 和 Azure ServiceBus 的替代品,我可以在其中生成 token 以授予远程服务对 ServiceHub 和 EventHub 资源的访问权限?

最佳答案

没有;客户端库不生成 SAS token 或连接字符串。一般来说,当前一代的 Azure SDK 仅支持 Azure 门户或 Azure CLI 返回的形式的 key 和连接字符串。

对于编程生成,生成它们的推荐方法记录在 here for Service Bus 中。和 here for Event Hubs ,尽管它们之间的格式实际上是通用的。

下面是一个 C# 实现示例:

public static string BuildSignature(
    string fullyQualifiedNamespace,
    string entityName,
    string sharedAccessKeyName,
    string sharedAccessKey,
    DateTimeOffset expirationTime)
{
    if (string.IsNullOrEmpty(fullyQualifiedNamespace))
    {
        throw new ArgumentException($" { nameof(fullyQualifiedNamespace) } cannot be null or empty.", nameof(fullyQualifiedNamespace));
    }

    if (string.IsNullOrEmpty(entityName))
    {
        throw new ArgumentException($" { nameof(entityName) } cannot be null or empty.", nameof(entityName));
    }

    if (string.IsNullOrEmpty(fullyQualifiedNamespace))
    {
        throw new ArgumentException($" { nameof(sharedAccessKeyName) } cannot be null or empty.", nameof(sharedAccessKeyName));
    }

    if (string.IsNullOrEmpty(fullyQualifiedNamespace))
    {
        throw new ArgumentException($" { nameof(sharedAccessKey) } cannot be null or empty.", nameof(sharedAccessKey));
    }
    
    using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(sharedAccessKey));

    var encodedAudience = WebUtility.UrlEncode(BuildAudience(fullyQualifiedNamespace, entityName));
    var expiration = Convert.ToString(ConvertToUnixTime(expirationTime), CultureInfo.InvariantCulture);
    var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes($"{ encodedAudience }\n{ expiration }")));

    return string.Format(CultureInfo.InvariantCulture, "{0} {1}={2}&{3}={4}&{5}={6}&{7}={8}",
        "SharedAccessSignature",
        "sr",
        encodedAudience,
        "sig",
        WebUtility.UrlEncode(signature),
        "se",
        WebUtility.UrlEncode(expiration),
        "skn",
        WebUtility.UrlEncode(sharedAccessKeyName));
}

private static string BuildAudience(
    string fullyQualifiedNamespace,
    string entityName)
{    
    var builder = new UriBuilder(fullyQualifiedNamespace)
    {
        Scheme = "amqps",
        Path = entityName,
        Port = -1,
        Fragment = string.Empty,
        Password = string.Empty,
        UserName = string.Empty,
    };

    builder.Path = builder.Path.TrimEnd('/');
    return builder.Uri.AbsoluteUri.ToLowerInvariant();
}

private static long ConvertToUnixTime(DateTimeOffset dateTimeOffset) =>
    Convert.ToInt64((dateTimeOffset - Epoch).TotalSeconds);

private static readonly DateTimeOffset Epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

就服务总线和事件中心而言,我认为可以支持 SAS 生成,因为 Azure 门户和 CLI 均不提供支持。不过,我们并未收到很多请求,也没有考虑考虑该功能。

如果您愿意,请随时open a feature request在 Azure SDK 存储库中并鼓励投票。这将帮助我们确定考虑的优先顺序。

关于azure - Azure.Messaging.ServiceBus 命名空间中是否有 CreateSharedAccessSignatureTokenProvider 的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73873177/

相关文章:

azure - 当消息到达 Azure Servicebus 中的死信队列时,如何设置警报

azure-data-lake - Azure 事件中心在启用 Data Lake Gen2 的情况下捕获到存储

c# - Azure 事件中心 - 如何在 .Net Core WebAPI 中实现使用者?

azure - Azure 容器实例成功/失败时发送电子邮件警报/日志

azure - 在 Azure 中,cscfg 文件来自哪里?

asp.net-mvc - 将 Web 角色发布到 Azure 云服务时的代码优先迁移

azure - 事件中心消息排队时间是否唯一?

azure - 无法从 Azure Active Directory 将用户添加到 VSTS

c# - .NET - 如何正确设置函数的 Azure 服务总线队列/主题

c# - 服务总线根据内容过滤消息