我的测试站点在部署后尝试从 azure blob 存储访问文件时开始收到 403 禁止返回。 这只是我们测试环境中的问题,新版本在生产中运行得很好。生产和测试都托管在 azure 中,并且都使用自己的 azure blob 存储。
我尝试重新生成 blob 存储的访问 key ,但没有成功。
我可以在本地使用访问 key 并连接到测试 blob 存储并访问文件。
如果我尝试更改测试环境以使用生产 blob 存储,我仍然会收到 403 禁止错误。
Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden. at System.Net.HttpWebRequest.GetResponse() at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 677 --- End of inner exception stack trace --- at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 604 at Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient.GetBlobReferenceFromServer(StorageUri blobUri, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobClient.cs:line 563 at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.GetBlobReferenceFromServer(String blobName, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs:line 818 at ASP.XYZ in d:\XYZ.cshtml:line 27 Request Information RequestID:fc137321-0001-00ce-02d2-de5736000000 RequestDate:Tue, 06 Jun 2017 14:41:21 GMT StatusMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
这肯定是运行测试站点的 azure 应用服务出现问题。我只是看不出它会是什么。
请参阅此处的要点以获取一些非常简化的代码 https://gist.github.com/Skaanning/5cddf95a0d1ff210482d99a683e0be9c .
编辑 我还有多个其他 blob 商店,但它们都不能在测试站点上运行。不过它们在其他环境下工作得很好。
我尝试放大或缩小它,但没有任何运气。但后来我尝试从 32 位更改为 64 位,现在它可以工作了!我将保留这个问题,以防有人可以解释这里到底发生了什么
最佳答案
var img = container.GetBlobReference("someimage.png"); // this works just fine
var img2 = container.GetBlobReferenceFromServer("someimage.png"); // this throws a 403
第一行可以正常工作。原因是当我们执行GetBlobReference方法时,它不会向服务器发送请求。当执行GetBlobReferenceFromServer方法时,它会向服务器发送请求以获取blob内容。如果在 blob 服务器中找不到 blob,则会抛出 404 not found 异常。
My test site has after a deploy started to get 403 forbidden back when trying to access files from the azure blob storage.
403禁止异常通常是由于访问 key 错误而导致的。请打印出您的测试环境中使用的连接字符串。
Trace.Write(CloudConfigurationManager.GetSetting("blob.storage"));
Azure 存储连接字符串使用以下格式。请确保您为连接字符串提供了正确的帐户名和 key 对。如果您为帐户名称2设置了帐户名称1和帐户 key ,则会导致异常。
DefaultEndpointsProtocol=https;AccountName=[accountName];AccountKey=[accountKey];EndpointSuffix=core.windows.net
导致此问题的另一件事是测试服务器拒绝了对存储服务器的请求。请检查您是否配置了Dynamic IP Security在您的 web.config 中。
编辑于2017年6月7日下午4:26
我建议你采纳我上面的建议。请在运行时打印出连接字符串。如果在 Azure 门户和 web.config 中的应用程序设置中设置连接字符串。 Azure 门户中的设置将覆盖 web.config 中的设置。
请同时在运行时打印出当前日期时间,以检查您的应用实例的系统时间是否正确。如果你的系统的日期或时间被改变,404 Forbidden也会发生。
存储服务确保请求到达服务时的时间不超过 15 分钟。这可以防止某些安全攻击,包括重放攻击。当此检查失败时,服务器返回响应代码 403(禁止)。
引用:Authentication for the Azure Storage Services
编辑于2017年6月7日下午5:55
您是否尝试在部署版本时删除测试服务器上的所有原始文件?
关于azure blob 使用正确的访问 key 返回 403 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44393468/