【问题标题】:WCF CORs Globel.asax VS web.configWCF CORs Globel.asax VS web.config
【发布时间】:2019-10-15 11:40:17
【问题描述】:

大家好,我的 Global.asax 文件中有以下内容:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS") {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Pragma, Cache-Control");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

我运行 WCF 并使用以下命令对其进行测试:

function ajaxGetData() {
    $.ajax({
        url: 'http://localhost:62755/TutorialService.svc/Tutorial',
        type: 'GET',
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            console.log(data);
        },
            failure: function (response) {
            alert(response.d);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("error: ", jqXHR);
        }
    });
}

function ajaxPostData() {
    $.ajax({
        url: 'http://localhost:62755/TutorialService.svc/Tutorial/1',
        type: 'POST',
        data: JSON.stringify({"Tutorialid": 1}),
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data, textStatus, jqXHR) {
            console.log(data);
        },
            failure: function (response) {
            alert(response.d);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("error: ", jqXHR);
        }
    });
}

这对 GETPOST 都适用。但是,如果我将上面的所有内容注释掉并使用 Web.config 并像 Global.asax 文件那样构建它:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Max-Age" value="1728000" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization, Pragma, Cache-Control" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>

它适用于获取 GET,但不适用于 POST。在控制台中发布错误说:

从源“http://localhost”访问“http://localhost:62755/TutorialService.svc/Tutorial/1”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。

我错过了什么?

【问题讨论】:

  • 我不认为这与您的问题有关,但您应该知道来源 * 不能与 auth 标头一起使用。请参阅MDN page on CORS。向下滚动到“凭据请求和通配符”。
  • 我有点回想起这个问题,我相信它与OPTIONSVerbHandler或添加IsapiModule有关
  • 确实,默认的web.config 添加了OPTIONSVerbHandler,它告诉IIS 忽略那个动词。
  • @penleychan 似乎你有点正确。如果我取出 它就像 Globala.sax 版本一样运行 :) 请随意将此作为官方答案。
  • 不用担心,我什至不知道如何在官方答案中解释这一点。 web.config 有时会做一些奇怪的事情。

标签: c# wcf cors access-control


【解决方案1】:

在我的例子中,我通过将以下代码添加到 global.asax 来解决我在 web.config 中配置 cors 的问题,以确保每个选项方法都有 200 个状态代码。

protected void Application_EndRequest(object sender, EventArgs e)
    {

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.StatusCode = 200;
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2011-03-02
    • 1970-01-01
    • 2018-04-24
    • 2014-10-04
    相关资源
    最近更新 更多