接口出现405跨域错误

2021-06-08  本文已影响0人  秒怂的哈士奇爱吃西瓜

web.config 文件

 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type,Accept,Authdealer, Authstr, Authuser" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    <!--未实施 HTTP 严格传输安全 (HSTS)-->
    <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
    <!--XSS漏洞-->
    <add name="X-Frame-Options" value="SAMEORIGIN" />
    <!--CSP漏洞-->
    <add name="Content-Security-Policy" value="default-src 'self' 'unsafe-inline' 'unsafe-eval' data:;connect-src frame-src 'self'; frame-ancestors 'self' " />
  </customHeaders>
</httpProtocol>

Global.asax文件
在用户会话启动后,每次发起的请求都会触发Application_BeginRequest事件,并在请求完成时触发Application_EndRequest事件。

      protected void Application_BeginRequest(object sender, EventArgs e)
    {
        //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
上一篇下一篇

猜你喜欢

热点阅读