1

我有兩個應用程序。一個是運行在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

回答

1

選項檢查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> 
3

嘗試使用與網絡電話

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

這從哪裏獲得憑據?我嘗試過但仍然收到錯誤消息。 – Alexander

+0

如果不是由服務直接管理,您需要將證書和證書頭一起傳遞 –

+0

如果證書不是由服務直接管理,那麼您需要將證書和證書頭一起傳遞 –

相關問題