ASP.net相同的原始策略标头不起作用

Tom*_*len 4 asp.net iis-7 cross-domain same-origin-policy iis-7.5

我收到错误:

XMLHttpRequest cannot load http://www.scirra.com/handlers/arcadeProcessScore.ashx. Origin http://static1.scirra.net is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)

arcadeProcessScore.ashx我行:

public void ProcessRequest (HttpContext context) {

    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://static1.scirra.net");
    context.Response.AppendHeader("Access-Control-Allow-Origin", "https://static1.scirra.net");
    context.Response.ContentType = "text/plain";
Run Code Online (Sandbox Code Playgroud)

但错误仍然存​​在.

我也尝试过:

context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
Run Code Online (Sandbox Code Playgroud)

哪个也行不通.

如果我添加<add name="Access-Control-Allow-Origin" value="*"/>web.config水平它的工作原理,但显然不是解决办法.

我怎样才能arcadeProcessScore.ashx接受来自的请求static1.scirra.net?谢谢你的帮助.

use*_*080 5

我做了一些我自己的测试,直接用一个XmlHttpRequest来访问我项目中的处理程序.我使用的设置是在我的本地IIS上发布应用程序(版本6.1,因此行为可能与7.5不同),并让Default.aspx页面调用我的处理程序在Visual Studio中的开发服务器中运行.像这样:

http://mymachine/WebTest/Default.aspx

-> XmlHttpRequest get request to

http://localhost:58025/WebTest/TestHandler.ashx
Run Code Online (Sandbox Code Playgroud)

处理程序中的代码:

public void ProcessRequest (HttpContext context) {
    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://mymachine");
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.ContentType = "text/plain";
    context.Response.Write("Hello World " + DateTime.Now.ToString());
}
Run Code Online (Sandbox Code Playgroud)

使用IE9,无论我是否Access-Control-Allow-Origin从处理程序发回标头,behviour都是相同的.IE9发出警告,要求用户确认是否应加载内容.

Chrome(版本21.0.1180.79 m)和FF(版本14.0.1)实际上都会向处理程序生成请求并尊重处理程序发回的标头.

所以这适用于Chrome和FF:

context.Response.AppendHeader("Access-Control-Allow-Origin", "http://mymachine");
Run Code Online (Sandbox Code Playgroud)

这样做了:

context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试在同一个响应中添加几个不同的允许来源,我无法让它们中的任何一个显示内容.对我来说,这些都不起作用:

  1. 添加几个响应标头

    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://mymachine");
    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://someothermachine");
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加一个标题,两个逗号分隔起源

    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://mymachine, http://someothermachine");
    
    Run Code Online (Sandbox Code Playgroud)
  3. 添加一个标题,两个原点空格分隔

    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://mymachine http://someothermachine");
    
    Run Code Online (Sandbox Code Playgroud)
  4. 添加一个标题,两个原点空格分隔

    context.Response.AppendHeader("Access-Control-Allow-Origin", "http://mymachine; http://someothermachine");
    
    Run Code Online (Sandbox Code Playgroud)

为了让它发挥作用,我所做的是遵循这个答案中给出的建议.我的处理程序看起来像这样:

public void ProcessRequest(HttpContext context)
{
    string[] allowedOrigins = new string[] { "http://mymachine", "http://someothermachine" };
    string origin = context.Request.Headers.Get("Origin");
    if (allowedOrigins.Contains(origin))
        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());
}
Run Code Online (Sandbox Code Playgroud)

有了这个,Chrome和FF都接受来自两个来源的处理程序的输出.