IE:使用启用CORS的.NET Core和Angular 2进行"Origin not found in Access-Control-Allow-Origin header"



我正在构建一个内部应用程序,使用Angular 2(CLI/Webpack)调用我使用.NET Core构建的启用CORS的服务。该服务使用用户的集成Windows身份验证凭据来查找有关该用户的信息,并将其返回给Angular。在Chrome和Firefox中一切都很好,但在IE 10和11中,我收到了一个"401未经授权"的回复,并显示消息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核心服务在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甚至没有显示凭据框。

如果有人遇到这个问题,我想我已经解决了。我的问题是我电脑的Internet选项,其中登录身份验证选项(Internet选项>安全>自定义级别…>用户身份验证>登录)设置为"仅在Intranet区域自动登录"。虽然这是有意的,IE并没有把我的网站看作是一个内联网网站,而是一个互联网网站。原因是IE使用的"点规则"。

"如果URI的主机名不包含任何句点(例如。http://team/)则将其映射到本地Intranet区域。"[1]

我们本地站点的主机名包含句点,因此映射到Internet区域。在我们能够突破繁文缛节之后,手动将我的站点添加到本地Intranet区域(Internet选项>安全>本地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调用的响应数据?

相关内容

最新更新