簡體   English   中英

部署時某些路由中不存在“Access-Control-Allow-Origin”header

[英]No “Access-Control-Allow-Origin” header is present in certain routes at deployment

I've build a frontend with Angular xx that picks up the routes of an existing ASP.NET Web API 1.x-Application.

身份驗證以及與后端的通信存在一些問題,因此我在客戶端實現了以下攔截器:

憑證的角攔截器:

@Injectable()
export class CredentialsInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {

    req = req.clone({
      withCredentials: true
    });

    return next.handle(req);
  }
}

為了處理現有的 CORS 沖突,我修改了 Global.asax,如下所示:

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string httpOrigin = HttpContext.Current.Request.Params["HTTP_ORIGIN"] ?? HttpContext.Current.Request.Params["ORIGIN"] ?? "*";
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", httpOrigin);
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Token, withCredentials");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");

            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.StatusCode = 200;
                var httpApplication = sender as HttpApplication;
                httpApplication.CompleteRequest();
            }
        }

web.config 像這樣:

<system.web>
  <authentication mode="Windows" />
  <authorization>
    <allow verbs="OPTIONS" users="*"/>
    <deny users="?"/>
  </authorization>
</system.web>

<system.webServer>
  <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

在我的本地計算機上,可以在localhost:80訪問后端(使用 IIS Express),而我將 Angular 應用程序托管在192.168.xxx.xx:80 over Z5DA5ACF461B4EFB27E76EC861065

在這種環境下,一切都運行良好,我沒有任何問題。

測試環境是具有 Windows Server 2008 R2 和 IIS 7.5 的服務器。 對於 Deep-Link-Usage URL-Rewriter 2 已安裝

在構建后端和前端並部署到上述環境之后,應用程序按預期工作,除了一個大缺陷。

IIS 中的后端地址為 *:80、*:8080 等。

IIS 中的 Frontend-Address 是 Server-IP:80

雖然某些路由按預期工作,但在本地設置中工作的其他一些路由正在產生以下 CORS 問題:

Access to XMLHttpRequest at *route* from origin *origin* has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No "Access-Control-Allow-Origin" header is present on the requested source.

到目前為止,我找不到工作的控制器和不工作的控制器之間有任何區別。

有誰知道這里發生了什么?

這么小的事情,卻有這么大的影響。

我忘記將<remove name="OPTIONSVerbHandler" />添加到web.config處理程序中。

現在一切都按預期工作。 希望它可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM