簡體   English   中英

IE:使用啟用CORS的.NET Core和Angular 2在“Access-Control-Allow-Origin標頭中找不到原點”

[英]IE: “Origin not found in Access-Control-Allow-Origin header” using CORS enabled .NET Core and Angular 2

我正在構建一個內部應用程序,我使用Angular 2(CLI / Webpack)來調用我使用.NET Core構建的啟用CORS的服務。 該服務使用用戶的集成Windows身份驗證憑據來查找有關該用戶的信息並將其返回給Angular。 在Chrome和Firefox中一切正常,但在IE 10和11中,我收到“401 Unauthorized”響應,並Origin http://localhost:4200 not found in Access-Control-Allow-Origin header.

在Angular中,我正在進行HTTP調用:

private options = new RequestOptions({withCredentials: true});    

let getURL = `server:port/api/users/username`;
return this.http
    .get(getURL, this.options)
    .map((response: Response) => response.json()[0])
    .catch(this.handleError);

我的.NET Core服務在Startup.cs中使用以下代碼:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("policyAnyone",
            builder => {
                builder.AllowCredentials()
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            });
    });

    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseCors("policyAnyone");

    app.UseApplicationInsightsRequestTelemetry();

    app.UseApplicationInsightsExceptionTelemetry();

    app.UseMvc();
}

然后,控制器使用通過User.FindFirst(ClaimTypes.Name).Value接收的用戶名,運行存儲過程,並返回結果。

作為參考,Chrome具有以下請求標頭:

Accept: application/json, text/plain, `*/*`
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Authorization: Negotiate %lotsOfEncodedText%
Cache-Control: max-age=0
Connection: keep-alive
content-type: text/plain
Host: server:port
Origin: http://localhost:4200
Referer: http://localhost:4200/dashboard
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36

和IE有這些:

Request: GET /api/users/username HTTP/1.1
Accept: application/json, text/plain, `*/*`
Content-Type: text/plain
Referer: http://localhost:4200/dashboard
Accept-Language: en-US
Origin: http://localhost:4200
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: server:port
Connection: Keep-Alive
Cache-Control: no-cache

似乎CORS配置正確,我的Angular設置非常簡單,但IE甚至沒有顯示憑據框。

如果有人遇到這個問題,我想我能夠解決問題。 我的問題在於我的PC的Internet選項,其中登錄驗證選項(Internet選項>安全性>自定義級別...>用戶驗證>登錄)設置為“僅在Intranet區域中自動登錄”。 雖然這是打算,但IE並沒有將我的網站視為內部網站,而是一個互聯網網站。 原因是IE使用的“點規則”。

“如果URI的主機名不包含任何句點(例如http:// team / ),那么它將映射到本地Intranet區域。” [1]

我們的本地站點的主機名包含句點,因此映射到Internet區域。 在我們能夠切斷繁文縟節之后,手動將我的站點添加到本地Intranet區域(Int​​ernet選項>安全性>本地Intranet>站點>高級)應該可以解決我的問題。

我不確定為什么IE決定用它所做的CORS ACAO錯誤消息做出回應,但是401:未經授權的響應至少足以讓我失意。

[1]互聯網上的信息選項: https//support.microsoft.com/en-us/help/258063/internet-explorer-may-prompt-you-for-a-password

[2]有關Intranet區域確定的信息: https//blogs.msdn.microsoft.com/ieinternals/2012/06/05/the-intranet-zone/

IE對端口的處理方式與其他瀏覽器不同。 其他瀏覽器說如果端口不同,那么它就不是“原點”了。 在IE中,如果端口不同但域相同,則它是相同的源並且不使用頭。

你可以在這里閱讀更多內容: https//developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#IE_Exceptions

但是你仍然應該通過Angular看到返回的數據(因為IE只會將它們視為相同的原點)。 那么你在ajax調用中看到了響應數據嗎?

暫無
暫無

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

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