Asp.net web api angular2:asp.net核心服务抛出';无访问控制允许原点';打开Windows身份验证时
我有任何Asp.net web api angular2:asp.net核心服务抛出';无访问控制允许原点';打开Windows身份验证时,asp.net-web-api,angular,Asp.net Web Api,Angular,我有任何angular2app访问asp.net核心webapi服务。如果webapi iis配置为(Properties\launchSettings.json),则它可以工作: 但是,当WindowsAuthentication为true且匿名身份验证为false时,它会抛出错误。错误: 无法加载XMLHttpRequest . 不 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许使用源“” 通道响应的HTTP状态代码为401 有什么想法
angular2
app访问asp.net核心webapi
服务。如果webapi iis配置为(Properties\launchSettings.json),则它可以工作:
但是,当WindowsAuthentication
为true
且匿名身份验证为false
时,它会抛出错误。错误:
无法加载XMLHttpRequest
. 不
“Access Control Allow Origin”标头出现在请求的服务器上
资源。因此,不允许使用源“”
通道响应的HTTP状态代码为401
有什么想法吗?您正试图提出跨来源请求。这在规范中是允许的,但需要配置 解决此问题有三个步骤
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseCors(builder =>
builder
.WithOrigins("http://localhost:4200") //<-- OP's origin
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
}
app.UseMvc();
}
public void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
if(env.IsDevelopment())
{
app.UseCors(builder=>
建设者
.来源(“http://localhost:4200“”//此.http.get(url,{withCredentials:true})的可能重复项引发另一个错误:XMLHttpRequest无法加载。凭据标志为“true”,但“访问控制允许凭据”标头为“”。必须为“true”才能允许凭据。因此,不允许访问源“”。@beewest有关ASP.NET核心web api的信息,请参阅此-@sanket:抱歉,但不起作用
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseCors(builder =>
builder
.WithOrigins("http://localhost:4200") //<-- OP's origin
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
}
app.UseMvc();
}
import {Injectable} from '@angular/core'
import {Headers, Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class SomeAngularServiceThatCallsYourAPI{
constructor(private http: Http) { }
getApiData(): Promise<MyDataResult[]> {
var apiUrl = 'http://localhost:12246/api//values/getSettings';
return this.http.get(apiUrl,{
withCredentials: true
})
.toPromise()
.then(response => this.extractData(response) as MyDataResult[])
.catch(this.handleError);
}
}