【问题标题】:CORS Headers appended to HTTP Response not Sent to Browser附加到 HTTP 响应的 CORS 标头未发送到浏览器
【发布时间】:2013-04-09 21:12:43
【问题描述】:

我通过HttpContext.Current 对象直接写入 ASP.NET MVC 中的请求标头,但这些标头并未发送到浏览器...知道可能导致这种情况的原因吗?如果我通过 web.config 添加它们,我只会得到标题。这对我不起作用,因为我需要允许多个域到 Access-Control-Allow-Origin

我尝试使用此代码将标题直接写入HttpContext.Current

context.Response.AppendHeader("Access-Control-Allow-Origin", origin);
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World " + DateTime.Now.ToString());

我得到了hello world,但没有得到标题。

我也尝试使用Thinktecture.IdentityModel.Http.Cors.WebApi,但得到相同的结果,这没什么。我已经检查并重新检查了我的代码,以确保它符合教程]1。我已经在 Web.config 中配置了标头,并通过 Thinktecture 进行了尝试,但是我仅在 Access-Control-Allow-Origin 位于 web.config 中时才获得标头,但在 Chrome/FF 中仍然出现错误。似乎标头仅在 OPTIONS 请求中发送,但我不确定。

Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ga;q=0.6,pt-BR;q=0.4,pt;q=0.2
Access-Control-Request-Headers:accept, origin, content-type
Access-Control-Request-Method:GET
Authorization:Negotiate REDACTED
Cache-Control:max-age=0
Connection:keep-alive
Host:bpapi.domain.com
Origin:http://dev-02
Referer:http://dev-02/_Layouts/PAR/NewParItem.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31


HTTP/1.1 200 OK
Server: Microsoft-IIS/7.5
Persistent-Auth: false
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: http://dev-02
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
WWW-Authenticate: Negotiate REDACTED
Date: Sat, 06 Apr 2013 00:35:31 GMT
Content-Length: 0

这里是web.config as a pastebin,以免混淆问题。 WebDAV没有安装。

public class CorsConfig
{
    public static void RegisterCors(HttpConfiguration httpConfig)
    {
        WebApiCorsConfiguration corsConfig = new WebApiCorsConfiguration();

        corsConfig.RegisterGlobal(httpConfig);

        corsConfig
                .ForResources(new string[] { "Industries", "Projects" })
                .ForOrigins(new string[] { "http://dev-01", "http://dev-02" })
                .AllowAll();
    }
}

这是 Thinktecture 代码:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        //CorsConfig.RegisterCors(GlobalConfiguration.Configuration);
        RegisterCors(MvcCorsConfiguration.Configuration);
    }
    private void RegisterCors(MvcCorsConfiguration corsConfig)
    {
        corsConfig
            .ForResources(new string[] {"Industries", "Projects" })
            .ForOrigins(new string[] { "http://dev-01", "http://dev-02" })
            .AllowAll();
    }

2013/04/09 更新:context.Response.AppendHeader(...)context.Response.AddHeader(...) 均无效。如果 Chrome 和 FF 无论是否允许原始标头都获得 JSONP,它们似乎都可以,因此我的项目至少可以工作。我也尝试过<remove name="OPTIONSVerbHandler"/>,但没有成功。我会将 MVC 应用程序部署到新服务器,以查看它是否已本地化到特定机器。

【问题讨论】:

  • 你有想过这个吗?
  • 在另一台服务器上一切正常。我最终为这个系统创建了一个 CORS 代理,因为这比将它部署到新服务器更容易。

标签: asp.net-mvc-4 asp.net-web-api iis-7.5 cors


【解决方案1】:

可能,您的麻烦是由于 OPTIONSVerbHandler。 详情见此链接:
http://eugeneagafonov.com/post/38312919044/iis-options-cors-aspnet-webapi-en

【讨论】:

  • 这是一个非常好的建议。实际上,我昨天已经得出了这个结论,并找到了确切的文章,但即使使用<remove name="OPTIONSVerbHandler"/>,我写给响应的任何标题都不会出现在 IIS 的另一端。我将部署到另一台服务器进行测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2019-10-23
  • 2012-02-06
  • 1970-01-01
  • 2015-04-26
  • 2015-07-22
  • 2023-01-26
相关资源
最近更新 更多