附加到HTTP响应的CORS标头未发送到浏览器

时间:2013-04-06 01:00:49

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

我通过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进行了尝试但是我只在{。1}}在web.config中时获得了标题,但我仍然在Chrome / FF中出错。似乎标题只是在OPTIONS请求中发送,但我不确定。

Access-Control-Allow-Origin

这是web.config as a pastebin,以免弄乱问题。 WebDAV 已安装。

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

这是Thinktecture代码:

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();
    }
}

更新2013/04/09: 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(); } context.Response.AppendHeader(...)都没有任何效果。如果他们获得JSONP而不管允许原始标题,那么Chrome和FF似乎没问题,所以我的项目至少可以工作。我也试过context.Response.AddHeader(...)但没有成功。我将MVC应用程序部署到新服务器,以查看它是否是本地化到特定计算机的东西。

1 个答案:

答案 0 :(得分:2)

可能是,你的烦恼是由于OPTIONSVerbHandler造成的。 有关详细信息,请参阅此链接:
http://eugeneagafonov.com/post/38312919044/iis-options-cors-aspnet-webapi-en