gpt4 book ai didi

Azure 存储 CORS 返回源通配符并在浏览器中失败

转载 作者:行者123 更新时间:2023-12-02 07:50:46 24 4
gpt4 key购买 nike

在 Azure Blob 存储上启用 CORS 时,几乎可以设置所有内容,但“...Allow-Credentials” header 除外,该 header 始终为 true。

因此,当对 origin-header 使用通配符时,飞行前请求可以正常工作并将通配符转换为实际的来源。

但后续的GET请求不会转换通配符并返回以下组合:

Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

这在 Chrome 中是非法的(可能其他浏览器也是如此)。错误是

XMLHttpRequest cannot load ...
A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true.
Origin 'http://localhost' is therefore not allowed access.

在新的 WebAPI v2 CORS 包中,通配符被替换为实际来源。另外,为什么在对 blob 存储的请求中需要 cookie 等凭据?最好把它关掉。

当我想使用原始通配符时,如何解决这个问题?

更新

这是我在 App_Start 上运行的初始化代码

public static void Initialize()
{
// Azure blob storage settings
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureStorage"].ConnectionString);
var client = storageAccount.CreateCloudBlobClient();
var serviceProperties = client.GetServiceProperties();
serviceProperties.Cors = new CorsProperties();

serviceProperties.Cors.CorsRules.Add(new CorsRule()
{
AllowedHeaders = new List<string>() { "*" },
AllowedOrigins = new List<string>() { "*" },
AllowedMethods = CorsHttpMethods.Get,
ExposedHeaders = new List<string>() { "*" },
MaxAgeInSeconds = 3600
});

client.SetServiceProperties(serviceProperties);
}

最佳答案

您遇到的错误是由于将 xmlhttpreqeust 上的 withCredentials 属性设置为 true 造成的,在这种情况下,浏览器将拒绝通配符 Access-Control-Allow-Origin。

In the new WebAPI v2 CORS package wildcards are replaced with the actual origin.

返回通配符是启用缓存的正确选择,请看以下场景:

  • 用户 A 向 MAS(Microsoft Azure 存储)上的公共(public) blob 发送 GET 请求。
  • 如果您使用 CDN/代理来缓存公共(public)资源(这是最佳实践),则 CDN 将缓存 Blob,并将 Access-Control-Allow-Origin 设置为“*”。
  • 现在,用户 B 向 MAS 发送相同的请求,并从缓存中获取响应,在这种情况下,由于缓存的 blob 具有通配符 Access-Control-Allow-Origin,浏览器将允许该请求,而您无需这样做命中 MAS 服务器。

现在,在您始终返回实际来源的另一种情况下,您无法为多个客户端缓存该资源,因为如果 Access-Control-Allow-Origin 的实际来源不同于请求原始 header 。

Also, why would I need credentials such as cookies in a request to the blob storage? Better turn it off.

您将需要凭据,因为发送经过身份验证的请求的一种方法是使用授权 header ,如果预检请求不允许这样做,那么浏览器应该使带有授权 header 的实际请求失败。

关于Azure 存储 CORS 返回源通配符并在浏览器中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23475143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com