Angular 4 - HttpClient - Cors - Windows 验证

Angular 4 - HttpClient - Cors - Windows Auth

我在本地 IIS 上托管了两个应用程序,例如 http://localhost/abc and http://localhost/xyz

abc 是一个 Angular 4 应用程序,xyz 是我的 Web API 后端 api 应用程序。

它们都是 Intranet 应用程序。用户将使用 Windows 身份验证登录到 http://localhost/abc

出于各种原因(包括审计和日志记录),我想以某种方式将用户的 windows 凭据传递到我的 Web Api 后端。因为它是一个以数据为中心的应用程序 - 捕获用户详细信息至关重要,否则它将无法工作。

我看过各种文章和问题,他们都提出了同样的建议。

在请求中添加 withCredentials。

我尝试这样做,但是从 Chrome - 我没有看到 withCredentials header 被设置,而在 IE 中至少我可以看到 header 使用 Fiddler 被设置。

我在网络上启用了 Cors Api 并且它在 http://localhost/abc 调用 http://localhost/xyz 方面确实有效,但我没有查看通过的凭据。

在 chrome 开发工具和 Fiddler 中,我没有看到 articles/tutorials 建议的请求设置了任何 cookie。

I get the following error in Chrome

{"Message":"An error has occurred.","ExceptionMessage":"The specified policy origin 'http://localhost/abc' is invalid. It must not contain a path, query, or fragment.","ExceptionType":"System.InvalidOperationException","StackTrace":"   at System.Web.Http.Cors.EnableCorsAttribute.ValidateOrigins(IList`1 origins)\r\n   at System.Web.Http.Cors.EnableCorsAttribute.GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Web.Http.Cors.CorsMessageHandler.<GetCorsPolicyAsync>d__10.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Cors.CorsMessageHandler.<HandleCorsRequestAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()"}

Web API Cors 启用

var cors = new EnableCorsAttribute("http://localhost/abc", "*", "*") { 
SupportsCredentials = true };

config.EnableCors(cors);

Web API开启windowsauth

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

Angular Http调用和设置withCredentials

let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, withCredentials: true });
return this.http.post(environment.backendApiUrl + 
    "/api/companies/search",jsonData, options)
    .map((response:Response) => <any[]>response.json())

备注 我还注意到,自从进行此更改以来;我的浏览器不再向我的服务器发出预检请求(选项)。这很奇怪,我相信这两件事有某种联系。

大家有什么想法吗?如果您需要查看更多代码片段,请告诉我。

TIA

删除片段。 CORS 关注域:

var cors = new EnableCorsAttribute("http://localhost", "*", "*") {