我想在Azure Blob Storage帐户中启用CORS。我正在尝试关注此示例https://code.msdn.microsoft.com/Windows-Azure-Storage-CORS-45e5ce76
服务器抛出System.Net.WebException(400)无效请求。
这是我的代码:
的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
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
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
答案 0 :(得分:0)
要启用CORS,您需要使用版本2013-08-15或更高版本为Blob,队列和表服务或版本2015-02-21或文件服务设置相应的服务属性。您可以通过将CORS规则添加到服务属性来启用CORS。
有关如何为服务启用或禁用CORS以及如何设置CORS规则的详细信息,请参阅Set Blob Service Properties
以下是通过“设置服务属性”操作指定的单个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>
答案 1 :(得分:0)
在与此斗争了将近一天之后,我发现该错误与不受支持的Azure Storage SDK版本有关。我降级到我的Microsoft.WindowsAzure.Storage SDK版本3.0.2.0,现在我可以和我的模拟器通话了。