[英]Enabling CORS for Azure Blob Storage
I want to enable CORS in my Azure Blob Storage account.我想在我的 Azure Blob 存储帐户中启用 CORS。 I am trying to follow this sample https://code.msdn.microsoft.com/Windows-Azure-Storage-CORS-45e5ce76
我正在尝试遵循此示例https://code.msdn.microsoft.com/Windows-Azure-Storage-CORS-45e5ce76
The server throws an System.Net.WebException (400) invalid request.服务器抛出 System.Net.WebException (400) 无效请求。
This is my code:这是我的代码:
Global.asax.cs Global.asax.cs
protected void Application_Start()
{
Database.SetInitializer<dynazzy.Models.DAL>(null);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AzureCommon.InitializeAccountPropeties();
}
AzureCommon.cs AzureCommon.cs
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure;
namespace dynazzy
{
/// <summary>
/// This class contains the Windows Azure Storage initialization and common functions.
/// </summary>
public class AzureCommon
{
private static CloudStorageAccount StorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
public static CloudBlobClient BlobClient
{
get;
private set;
}
public static CloudTableClient TableClient
{
get;
private set;
}
public static CloudBlobContainer ImagesContainer
{
get;
private set;
}
public const string ImageContainerName = "someimagescontainer";
/// <summary>
/// Initialize Windows Azure Storage accounts and CORS settings.
/// </summary>
public static void InitializeAccountPropeties()
{
BlobClient = StorageAccount.CreateCloudBlobClient();
TableClient = StorageAccount.CreateCloudTableClient();
InitializeCors(BlobClient, TableClient);
ImagesContainer = BlobClient.GetContainerReference(AzureCommon.ImageContainerName);
ImagesContainer.CreateIfNotExists(BlobContainerPublicAccessType.Container);
}
/// <summary>
/// Initialize Windows Azure Storage CORS settings.
/// </summary>
/// <param name="blobClient">Windows Azure storage blob client</param>
/// <param name="tableClient">Windows Azure storage table client</param>
private static void InitializeCors(CloudBlobClient blobClient, CloudTableClient tableClient)
{
// CORS should be enabled once at service startup
ServiceProperties blobServiceProperties = new ServiceProperties();
ServiceProperties tableServiceProperties = new ServiceProperties();
// Nullifying un-needed properties so that we don't
// override the existing ones
blobServiceProperties.HourMetrics = null;
tableServiceProperties.HourMetrics = null;
blobServiceProperties.MinuteMetrics = null;
tableServiceProperties.MinuteMetrics = null;
blobServiceProperties.Logging = null;
tableServiceProperties.Logging = null;
// Enable and Configure CORS
ConfigureCors(blobServiceProperties);
ConfigureCors(tableServiceProperties);
// Commit the CORS changes into the Service Properties
blobClient.SetServiceProperties(blobServiceProperties);
tableClient.SetServiceProperties(tableServiceProperties);
}
/// <summary>
/// Adds CORS rule to the service properties.
/// </summary>
/// <param name="serviceProperties">ServiceProperties</param>
private static void ConfigureCors(ServiceProperties serviceProperties)
{
serviceProperties.Cors = new CorsProperties();
serviceProperties.Cors.CorsRules.Add(new CorsRule()
{
AllowedHeaders = new List<string>() { "*" },
AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post,
AllowedOrigins = new List<string>() { "*" },
ExposedHeaders = new List<string>() { "*" },
MaxAgeInSeconds = 1800 // 30 minutes
});
}
}
}
WebApiConfig.cs WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
To enable CORS, you need to set the appropriate service properties using version 2013-08-15 or later for the Blob, Queue, and Table services, or version 2015-02-21 or for the File service.要启用 CORS,您需要使用版本 2013-08-15 或更高版本为 Blob、队列和表服务或版本 2015-02-21 或文件服务设置适当的服务属性。 You enable CORS by adding CORS rules to the service properties.
您可以通过将 CORS 规则添加到服务属性来启用 CORS。
For details about how to enable or disable CORS for a service and how to set CORS rules, please refer to Set Blob Service Properties有关如何为服务启用或禁用 CORS 以及如何设置 CORS 规则的详细信息,请参阅设置 Blob 服务属性
Here is a sample of a single CORS rule, specified via a Set Service Properties operation:以下是通过设置服务属性操作指定的单个 CORS 规则示例:
<Cors>
<CorsRule>
<AllowedOrigins>http://www.contoso.com, http://www.fabrikam.com</AllowedOrigins>
<AllowedMethods>PUT,GET</AllowedMethods>
<AllowedHeaders>x-ms-meta-data*,x-ms-meta-target*,x-ms-meta-abc</AllowedHeaders>
<ExposedHeaders>x-ms-meta-*</ExposedHeaders>
<MaxAgeInSeconds>200</MaxAgeInSeconds>
</CorsRule>
<Cors>
After fighting with this for almost one day, I figured out that the error relates to an unsupported Azure Storage SDK version.在与此斗争了将近一天后,我发现该错误与不受支持的 Azure 存储 SDK 版本有关。 I downgraded to version 3.0.2.0 of my Microsoft.WindowsAzure.Storage SDK and now I can talk to my Emulator.
我将 Microsoft.WindowsAzure.Storage SDK 降级到 3.0.2.0 版,现在我可以与我的模拟器对话了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.