问题描述
前文提要:【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
在前几天的实验中,使用了App Service作为反向代理,把默认的存储账号的域名修改为自定义的域名后,在 .NET Storage Account SDK中实现常规的上传/下载操作。
后面,想到需要把SAS Token直接写入代码中,就算是使用配置文件来保存SAS Token,也会存在如下问题:
1)安全性问题,如果SAS Token被泄漏呢?
2)SAS Token需要频繁的更改,难道每一次更改都需要重启应用吗?
想要避免这样的问题,是否可以在反向代理中,把SAS Token的内容追加在新的URL中呢?
问题解答
经过试验,是可以的。
在.NET Storage Account SDK中的BlobServiceClient对象,使用Uri作为构造函数参数,代码中并没有解析这个Uri中的验证参数信息,它通过直接传递到Storage Account服务。
基于这个判断,是可以把SAS Token信息隐藏,然后通过反向代理添加在后端转发的请求中。而在SDK中,只需要一个带blob name 或container name的Uri就可以了。
对以上方案修改的步骤有二:
第一步:修改 App Service 的 web.config Rewrite规则
在 rewrite action 的 url 中,直接把SAS Token内容添加在 {R:1} 参数之后。
注意:一定要把( &) 符号 替换为 ( & ), 否则会因为 web.config 中的内容格式不对导致 App Service 报错 500
<configuration> <system.webServer> <rewrite> <rules> <rule name="root" stopProcessing="true"> <match url="^/?(.*)" /> <action type="Rewrite" url="https://****.blob.core.chinacloudapi.cn/{R:1}?sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupitfx&se=2025-03-08T14:52:33Z&st=2025-03-08T06:52:33Z&spr=https&sig=***********" appendQueryString="true" /></rule> </rules> </rewrite> </system.webServer> </configuration>
第二步:在SDK中去掉 Uri 中的SAS Token
////Use SAS URL
//string accountsas = "https://<the app gateway domain name>/?sv=2022-11-02&*****https&sig=PxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxD";//修改为,如使用App Service,则为:https://xxxxxxxxxxxxxxx.chinacloudsites.cn/ string accountsas = "https://<the app gateway domain name>/";Uri sasuri = new Uri(accountsas); BlobServiceClient blobServiceClient = new BlobServiceClient(sasuri);
代码执行结果:
参考资料
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作: https://www.cnblogs.com/lulight/p/18745481