Angular 4 - HttpClient - Cors - Windows Auth



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

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

它们都是内部网应用。 用户将使用 Windows 身份验证登录到 http://localhost/abc。

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

我看过各种各样的文章和问题,它们都提出了同样的事情。

将 withCredentials 添加到请求中。

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

我已经在 web API 上启用了 Cors,它在 http://localhost/abc 调用 http://localhost/xyz 方面确实有效,但我没有看到凭据被传递。

在 chrome 开发工具和 Fiddler 中,我没有看到任何 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)rn   at System.Web.Http.Cors.EnableCorsAttribute.GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)rn   at System.Web.Http.Cors.CorsMessageHandler.<GetCorsPolicyAsync>d__10.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()rn   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn   at System.Web.Http.Cors.CorsMessageHandler.<HandleCorsRequestAsync>d__5.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()rn   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn   at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()"}

Web API Cors enable

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

Web API 打开窗口身份验证

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

Angular Http 调用和设置与凭据

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())

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

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

蒂亚

删除片段。 CORS关心域名:

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

最新更新