Calling Web Service using Integrated Windows Authentication in Angular 2

狂风中的少年 提交于 2019-12-24 07:25:16

问题


I have got two applications. One is an ASP.NET Core Web API REST Web Service that is running at http://localhost:59944 and the other one is an ASP.NET Core application using Angular 2 running at http://localhost:63888/. I want to load JSON Data via the REST Web Service.

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

Status Code 401 seems to be unauthorized exception.

The Web API Project is using integrated Windows authentication. If I call the Web API method via the browser it works fine. If I call it via Angular 2 I get the error message.

I have configured ASP.NET Core to allow CORS like this:

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

The web Ssrvice method call in Angular 2 looks like this:

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

How can I do the authentication? When using Internet Explorer and calling the web service directly I do not have to enter my username and password because the browser uses the credentials of the currently logged in user to authenticate. Is it possible to do this with web Service calls via Angular 2? I mean is it possible to use the web service without the user having to enter his username and Password by using Windows integrated authentication?


回答1:


Check Views\Home\Index.cshtml and replace:

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

with

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

or remove asp-prerender-module if not required:

<app>Loading...</app>



回答2:


Try using options with web call

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


来源:https://stackoverflow.com/questions/44195858/calling-web-service-using-integrated-windows-authentication-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!