在使用ASP.NET的Windows Azure Blob存储集CORS [英] Set CORS on Windows Azure blob storage using ASP.NET

查看:155
本文介绍了在使用ASP.NET的Windows Azure Blob存储集CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Windows Azure Blob存储帐户设置CORS属性。我使用的是ASP.NET服务器发送PUT请求。

I'm attempting to set CORS properties on my Windows Azure blob storage account. I'm using an ASP.NET server to send the PUT request.

服务器发回禁止回应,称服务器无法进行身份验证的要求。确保授权头的值是正确形成,包括签名。

The server is sending back a Forbidden response, saying "Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."

所以它必须是东西在我的认证头。这里有两个功能我用得到的标题。

So it must be something in my authentication header. Here are the two functions I use to get the header.

public string GetWindowsAzureAuthenticationHeader(string verb)
{
    string stringToSign = String.Format("{0}\n"
                                        + "\n" // content encoding
                                        + "\n" // content language
                                        + "\n" // content length
                                        + "\n" // content md5
                                        + "\n" // content type
                                        + "\n" // date
                                        + "\n" // if modified since
                                        + "\n" // if match
                                        + "\n" // if none match
                                        + "\n" // if unmodified since
                                        + "\n" // range
                                        + "x-ms-date:" + DateTime.UtcNow.ToString("R") + "\nx-ms-version:2013-08-15\n" // headers
                                        + "/{1}\ncomp:properties\nrestype:service", verb, CloudConfig.StorageAccountName);

    return SignThis(stringToSign, CloudConfig.StorageAccountKey, CloudConfig.StorageAccountName);
}

private string SignThis(string stringToSign, string key, string account)
{
    string signature;
    var unicodeKey = Convert.FromBase64String(key);
    using (var hmacSha256 = new HMACSHA256(unicodeKey))
    {
        var dataToHmac = Encoding.UTF8.GetBytes(stringToSign);
        signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
    }

    String authorizationHeader = String.Format(
        CultureInfo.InvariantCulture,
        "{0} {1}:{2}",
        "SharedKey",
        account,
        signature);

    return authorizationHeader;
}

这是发送请求控制器动作。该_mediaFactory.GetWindowsAzureCors方法返回一个XML文件的内容与我的CORS请求。

This is the controller action that sends the request. The _mediaFactory.GetWindowsAzureCors method returns the contents of an XML file with my CORS request.

var content = Encoding.UTF8.GetBytes(_mediaFactory.GetWindowsAzureCors(ControllerContext.HttpContext.Server));
var request = (HttpWebRequest)WebRequest.Create(CloudConfig.StorageAccountUri);

request.Method = "PUT";
request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R"));
request.Headers.Add("x-ms-version", "2013-08-15");
request.ContentType = "text/plain; charset=UTF-8";
request.Host = string.Format("{0}.blob.core.windows.net", CloudConfig.StorageAccountName);
request.Headers.Add("Authorization", _mediaFactory.GetWindowsAzureAuthenticationHeader(request.Method));

request.GetRequestStream().Write(content, 0, content.Length);
using (var response = (HttpWebResponse) request.GetResponse())
{
    model.StatusCode = response.StatusCode;
    model.Response = response.StatusDescription;
}

我在做什么错了?

What am I doing wrong?

推荐答案

它实际上是pretty简单。

It's actually pretty simple.

首先,在你的项目中添加引用Azure存储客户端库。如果您使用的NuGet,你将要安装的包是<一个href=\"http://www.nuget.org/packages/WindowsAzure.Storage/\"><$c$c>WindowsAzure.Storage.

First, add reference to Azure Storage Client library in your project. If you're using Nuget, the package you would want to install is WindowsAzure.Storage.

在此之后,这台CORS设置功能是<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobclient.setserviceproperties%28v=azure.10%29.aspx\"><$c$c>SetServiceProperties.这里的样本code这样做的:

After that, the function which sets the CORS settings is SetServiceProperties. Here's the sample code to do so:

            CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey), true);
            var blobClient = storageAccount.CreateCloudBlobClient();
            ServiceProperties blobServiceProperties = new ServiceProperties();
            blobServiceProperties.Cors.CorsRules.Add(new CorsRule(){
                AllowedHeaders = new List<string>() {"*"},
                ExposedHeaders = new List<string>() {"*"},
                AllowedMethods = CorsHttpMethods.Post | CorsHttpMethods.Put | ... Other Allowed Methods,
                AllowedOrigins = new List<string>() {"http://yourdomain.com", "https://yourdomain.com", "blah", "blah", "blah"},
                MaxAgeInSeconds = 3600,
            });
            blobClient.SetServiceProperties(blobServiceProperties);

这篇关于在使用ASP.NET的Windows Azure Blob存储集CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆