首页 文章

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

提问于
浏览
1

我有两个申请 . 一个是运行在http://localhost:59944的ASP.NET Core Web API REST Web服务,另一个是使用运行在http://localhost:63888/的Angular 2的ASP.NET Core应用程序 . 我想通过REST Web服务加载JSON数据 .

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 Core以允许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();
    }

Angular 2中的Web Ssrvice方法调用如下所示:

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

我该如何进行身份验证?使用Internet Explorer并直接调用Web服务时,我不必输入用户名和密码,因为浏览器使用当前登录用户的凭据进行身份验证 . 是否可以通过Angular 2进行Web服务调用?我的意思是可以使用Web服务而无需用户使用Windows集成身份验证输入用户名和密码?

2 回答

  • 3

    检查 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>
    
  • 1

    尝试使用Web调用选项

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

相关问题