我构建了一个 ASP.NET Web API 服务并在该服务中启用了 CORS。此服务用于提供报告模板资源(html、图像、css、字体)
。 Web客户端加载模板并根据下载的模板显示报告。
因此,给定服务点:http://templates.domain.com
,
我尝试从 Web 应用程序 (http://client.domain.com
) 访问服务 (REST, Image, Font)
,然后 Web 客户端应用程序将加载:
http://templates.domain.com/templates/:templateName
http://templates.domain.com/templates/:templateName/css/style.css
http://templates.domain.com/templates/:templateName/image/header.jpg
http://templates.domain.com/templates/:templateName/font/test.ttf
在上面,服务中的 REST API、CSS 和图像运行良好,但字体被阻止/失败。
Font from origin 'http://localhost:49350' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null'
到目前为止,我已经尝试了以下解决方案,但字体仍然被阻止。
Microsoft.Owin.Cors
:app.UseCors(CorsOptions.AllowAll);
Microsoft.AspNet.WebApi.Cors
:var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors);
最佳答案
您使用的是 OWIN 还是 WebAPI?
对于 AspNet WebAPI,以下内容将允许一切通过:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
需要指出的是,允许“*”是潜在的安全漏洞,因为您说任何地方的任何人都可以调用这些方法。
关于ASP.NET CORS 阻止字体请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32519603/