问题描述
在 Azure 存储帐户中,我已经开始使用 SAS(共享访问签名)和 SAP(存储访问策略)来保护对 Azure 存储队列中特定队列的访问。
我想要实现的是将特定 IP 限制到特定队列(1.1.1.1 可以访问 queueA,但 2.2.2.2 不能)。
目前我已经看到我可以使用存储帐户级别 SAS 来限制 IP,以及在门户的网络部分设置限制。这些还不够。
(我知道以下问题,但对回答不满意,其中说尝试设置存储帐户的网络 - Is it possible to filtre on IP address for Azure STORAGE SAS with ACCESS POLICY?)
谢谢
解决方法
您可以使用代码为该队列创建一个 service SAS token
(例如名为 queueA 的队列),然后将其与 Stored Access Policy
相关联。
例如(请修改代码以满足您的需要):
QueueClient queueClient = new QueueClient(connectionString,"queueA");
//create a service SAS
QueueSasBuilder sasBuilder = new QueueSasBuilder()
{
QueueName = "queueA",//set the ip here
IPRange = new SasIPRange(IPAddress.Parse("172.16.0.1"))
};
//associate the service SAS with the Stored Access Policy
sasBuilder.Identifier = storedPolicyName;
//then you can use this uri with sas token to operate this queue
Uri sasUri = queueClient.GenerateSasUri(sasBuilder);
更多细节可以参考this article(它是针对 blob 存储的,但是你可以很容易地修改它用于队列存储)。