我在 .Net Core 2.0 中使用 Microsoft.WindowsAzure.Storage C# 库。 使用此库,我尝试在 Azure 存储模拟器中动态配置 CORS,但出现错误:
"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature".
public async void ConfigureCors() {
ServiceProperties serviceProperties = await this.blobClient.GetServicePropertiesAsync();
serviceProperties.Cors = new CorsProperties();
serviceProperties.Cors.CorsRules.Clear();
serviceProperties.Cors.CorsRules.Add(new CorsRule() {
AllowedHeaders = allowedCorsHeaders,
ExposedHeaders = allowedCorsExposedHeaders,
AllowedOrigins = allowedCorsOrigin,
AllowedMethods = allowedCorsMethods,
MaxAgeInSeconds = allowedCorsAgeInSeconds
});
await blobClient.SetServicePropertiesAsync(serviceProperties);
}
我能够生成 SAS key 以直接在本地服务器上上传文件,但无法动态配置 CORS 以便我可以通过 C# 代码访问存储。
需要注意的奇怪的事情是,使用 Azure 存储云时上述代码运行得非常好,但本地模拟器抛出此错误。
版本信息:
WindowsAzure.Storage版本是8.4.0
Windows Azure 存储模拟器版本 5.2.0.0
Azure 存储资源管理器版本为 0.9.01
用于连接的凭据:
帐户名称=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
最佳答案
根据你的描述,我已经创建了一个测试演示。效果很好。
我猜您收到服务器无法验证请求
错误的原因是错误的azure存储包版本和存储模拟器版本。
建议您先将存储版本更新到8.4.0,将存储模拟器版本更新到5.2,然后再尝试。
有关我的测试演示的更多详细信息,您可以引用以下代码:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
ServiceProperties serviceProperties = blobClient.GetServicePropertiesAsync().Result;
serviceProperties.Cors = new CorsProperties();
serviceProperties.Cors.CorsRules.Clear();
serviceProperties.Cors.CorsRules.Add(new CorsRule()
{
AllowedHeaders = new List<string>() { "*" },
ExposedHeaders = new List<string>() { "*" },
AllowedOrigins = new List<string>() { "*" },
AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post,
MaxAgeInSeconds = 1800
});
var re = blobClient.SetServicePropertiesAsync(serviceProperties);
结果:
关于c# - Azure 存储模拟器 - 动态配置 CORS 会引发服务器身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46951170/