【问题标题】:SEC7123: Request header Key was not present in the Access-Control-Allow-Headers listSEC7123:访问控制允许标头列表中不存在请求标头密钥
【发布时间】:2019-08-25 19:32:42
【问题描述】:

我正在尝试将自定义标头从角度 HTTP 拦截器传递给我的 Web API。 我已将 web.config 中允许的标头列表中的标头添加为

 <add name="Access-Control-Allow-Origin" value="*" />
 <add name="Access-Control-Allow-Headers" value="Key,Content-Type" />
 <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />

这在 Chome 中运行良好,但在 Microsoft Edge 中它给了我以下错误:

SEC7123: Request header Key was not present in the Access-Control-Allow-Headers list.

我还缺少其他设置吗?

【问题讨论】:

标签: c# angular asp.net-web-api cors microsoft-edge


【解决方案1】:

我可以通过在 Global.asax 文件的 BeginRequest 方法中添加以下代码来解决此问题

protected void Application_BeginRequest (Object sender, EventArgs e) 
{
    if (Request.Headers.AllKeys.Contains ("Origin") && Request.HttpMethod == "OPTIONS") 
    {
        Context.Response.AddHeader ("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Key, Accept,Authorization,serverName");
        Context.Response.AddHeader ("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        Context.Response.End ();
    }
}

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 2023-03-11
    • 2022-06-25
    • 2016-03-15
    • 2018-01-23
    • 2017-08-31
    • 2020-10-31
    • 2015-05-24
    相关资源
    最近更新 更多