c# - Azure 存储模拟器 - 动态配置 CORS 会引发服务器身份验证错误

标签 c# azure asp.net-core azure-storage

我在 .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=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuF‌​q2UVErCz4I6tq/K1SZFP‌​TOtr/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);

结果:

enter image description here

关于c# - Azure 存储模拟器 - 动态配置 CORS 会引发服务器身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46951170/

相关文章:

powershell - 当我尝试通过 CLI 部署 Azure 云服务时出现 "Object reference not set to an instance of an object"

.net - 从 Controller 返回 XDocument (dotnet coreclr)

c# - C# HTTP PUT 请求代码的问题

c# - mongodb C# 驱动程序更新多个字段

azure - Windows Azure 中的 Windows 服务

reactjs - 在 ASP.NET Core API 中处理 id_token

c# - 如何在 asp.net core web api 中绑定(bind) Json 查询字符串

c# - 使用 SendGrid v3 的身份将交易模板作为确认电子邮件发送

c# - 如何加速 Winforms ListView 项目删除?

azure - 自动调整azure VM大小的简单方法