加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:23:55 所属栏目:asp.Net 来源:网络整理
导读:我收到错误: XMLHttpRequest cannot load http://www.scirra.com/handlers/arcadeProcessScore.ashx. Origin http://static1.scirra.net is not allowed by Access-Control-Allow-Origin. 在arcadeProcessScore.ashx上我有以下几行: public void ProcessRe
我收到错误:

XMLHttpRequest cannot load http://www.scirra.com/handlers/arcadeProcessScore.ashx. Origin http://static1.scirra.net is not allowed by Access-Control-Allow-Origin.

在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";

但错误仍然存??在.

我也尝试过:

context.Response.AppendHeader("Access-Control-Allow-Origin","*");

哪个也行不通.

如果我添加< add name =“Access-Control-Allow-Origin”value =“*”/>在web.config级别它可以工作,但显然不是解决方案.

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

解决方法

我做了一些我自己的测试,直接使用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

处理程序中的代码:

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

使用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");

这样做了:

context.Response.AppendHeader("Access-Control-Allow-Origin","*");

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

>添加几个响应标头

context.Response.AppendHeader("Access-Control-Allow-Origin","http://mymachine");
context.Response.AppendHeader("Access-Control-Allow-Origin","http://someothermachine");

>添加一个标题,两个逗号分隔起源

context.Response.AppendHeader("Access-Control-Allow-Origin","http://mymachine,http://someothermachine");

>添加一个标题,两个原点空格分隔

context.Response.AppendHeader("Access-Control-Allow-Origin","http://mymachine http://someothermachine");

>添加一个标题,"http://mymachine; http://someothermachine");

为了使它工作,我所做的是遵循this answer中给出的建议.我的处理程序然后看起来像这样:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读