在Angular 2中使用集成Windows身份验证调用Web服务

问题描述:

我有两个应用程序。一个是运行在http://localhost:59944上的ASP.NET Core Web API REST Web服务,另一个是使用运行在http://localhost:63888/上的Angular 2的ASP.NET Core应用程序。 我想通过REST Web服务加载JSON数据。在Angular 2中使用集成Windows身份验证调用Web服务

Exception: Call to Node module failed with error: Response with status: 401 null for URL: 
Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance+<InvokeExportAsync>d__7.MoveNext() 

状态码401似乎是未经授权的例外。

Web API项目正在使用集成Windows身份验证。 如果我通过浏览器调用Web API方法,它工作正常。如果我通过Angular 2调用它,我会收到错误消息。

我已经配置ASP.NET核心,以允许这样的CORS:

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

     app.UseCors(builder => 
      builder.WithOrigins("http://localhost:63888")); 

     app.UseMvc(); 
    } 

在角2的网络Ssrvice方法调用看起来是这样的:

this.http.get('http://localhost:59944/api/myWebServiceMethod) 
     .map((response: Response) => response.json()) 
     .subscribe(peopleStatistics => { 
      return peopleStatistics; 
     }); 

我该怎么办认证? 当使用Internet Explorer并直接调用Web服务时,我不必输入用户名和密码,因为浏览器使用当前登录用户的凭据进行身份验证。是否有可能通过Angular 2的Web Service调用完成此操作?我的意思是可以使用Web服务,而无需用户使用Windows集成身份验证输入他的用户名和密码?

+1

Angular 2解决方案使用'asp-prerender-module'(如果是的话,它应该在'Home \ Index.cshtml'中配置)? – Alexei

+0

使用asp-prerender-module的解决方案: 正在加载... 我现在将其更改为正在加载...。 之后,Web服务调用工作。 – Alexander

选项检查Views\Home\Index.cshtml和替换:

<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app> 

<app asp-ng2-prerender-module="ClientApp/dist/main-server">Loadin‌​g...</app> 

或删除asp-prerender-module如果不需要的话:

<app>Loading...</app> 

尝试使用与网络电话

let options = new RequestOptions({ headers: headers, withCredentials: true }); 
+0

这从哪里获得凭据?我尝试过但仍然收到错误消息。 – Alexander

+0

如果不是由服务直接管理,您需要将证书和证书头一起传递 –

+0

如果证书不是由服务直接管理,那么您需要将证书和证书头一起传递 –