Angular 4 Odata (ASP.net) Windows Chrome 身份验证

Angular 4 Odata (ASP.net) Windows Authentication on Chrome

我有一个 Angular 4 服务调用 OData API。我的 API 设置可以使用 Windows 身份验证工作,这可以通过 IE(版本 11)工作,但不能用于 Chrome,它会抛出一个:

Failed to load resource: the server responded with a status of 401 (Unauthorized)

接下来是:

XMLHttpRequest cannot load http://gbldnrgaptest1:53219/User. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://gap' is therefore not allowed access. The response had HTTP status code 401.

我使用以下方法为每个发送的请求创建选项:

private GetOptions(): RequestOptionsArgs{
    let options : RequestOptionsArgs;
    options = {
        headers : this.GetHeaders(),
        withCredentials: true
    };

    return options;
}

private GetHeaders(): Headers {
    let headers = new Headers();
    headers.append("OData-Version","4.0");
    headers.append("Content-Type","application/json;odata.metadata=minimal");
    headers.append("Accept","application/json");
    return headers;
}

在我的 API 上,我启用了 CORS:

var cors = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*", exposedHeaders: "*")
    {
        SupportsCredentials = true
    };
    config.EnableCors(cors);       

我还在 web.config:

中启用了 Windows 身份验证
<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
  <authentication mode="Windows"></authentication>
</system.web>

我的IIS也设置为只允许Windows Auth:

有人知道我可能遗漏了什么吗?

我觉得这是一个 header 问题;当我只是 运行 在 Chrome 中获取请求时,例如通过导航到 http://gbldnrgaptest1:53219/User 它 returns 很好,一旦我添加了 NTLM 身份验证,SOAP UI 也可以工作。

让它工作,我必须执行以下操作:

将以下内容添加到 Web.Config:

<authorization>
  <allow verbs="OPTIONS" users="*"/>
  <deny users="?" />
</authorization>

这是因为发出的飞行前请求显然没有发送凭据,这导致了错误的发生。 既然设置了上述规则 匿名身份验证 需要重新打开; Web.Config 中设置的规则将过滤经过的内容,只允许匿名完成 OPTIONS 请求。