Angularjs 使用凭据启用Cors.webapi核心1.1
我正在使用WebAPI核心1.1和angular 2。 我在每个HTTP请求中使用令牌和https等凭据。在Internet explorer中启动时一切正常,但在使用chrome和firefox时,获取凭据和Http请求时出错。有人能告诉我为什么我会出错吗?谢谢Angularjs 使用凭据启用Cors.webapi核心1.1,angularjs,asp.net-web-api,asp.net-core,cors,Angularjs,Asp.net Web Api,Asp.net Core,Cors,我正在使用WebAPI核心1.1和angular 2。 我在每个HTTP请求中使用令牌和https等凭据。在Internet explorer中启动时一切正常,但在使用chrome和firefox时,获取凭据和Http请求时出错。有人能告诉我为什么我会出错吗?谢谢 public void ConfigureServices(IServiceCollection services) { ... services.AddCors(opt => { opt.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddCors(opt =>
{
opt.AddPolicy("MyCorsPolicy", builder =>
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
services.AddMvc();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
app.UseCors("MyCorsPolicy");
app.UseMvc();
...
}
我的ng2代码:
import {Injectable, OnInit} from '@angular/core'
import {Headers, Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class MyApiCallService implements OnInit{
constructor(private http: Http) { }
ngOnInit() {
this.fetchCredentials
.subscribe(c => this.token = c
,error => { console.log(error) });
}
private fetchCredentials(): Observable<any> {
var apiUrl = 'http://localhost:2762/api/someEndpoint';
return this.http.get(apiUrl,{ withCredentials: true })
.map(response => response.json())
.catch(this.handleError);
}
}
从'@angular/core'导入{Injectable,OnInit}
从'@angular/Http'导入{Headers,Http,Response};
从“rxjs/Observable”导入{Observable};
@可注射()
导出类MyApiCallService实现OnInit{
构造函数(私有http:http){}
恩戈尼尼特(){
这是我的证书
.subscribe(c=>this.token=c
,error=>{console.log(error)});
}
私有fetchCredentials():可观察{
var apiUrl=http://localhost:2762/api/someEndpoint';
返回this.http.get(APIRL,{withCredentials:true})
.map(response=>response.json())
.接住(这个.把手错误);
}
}
默认情况下,它将清除所有自定义标题
您必须对ExposedHeader(“HeaderName”)使用
例如:
app.UseCors(options =>
{
options.AllowAnyHeader();
options.WithExposedHeaders("X-Pagination");
options.AllowAnyOrigin();
options.AllowAnyMethod();
});
因此,当您从客户端进行HTTP调用时,您将能够看到您的头。默认情况下,它将清除所有自定义头 您必须对ExposedHeader(“HeaderName”)使用
例如:
app.UseCors(options =>
{
options.AllowAnyHeader();
options.WithExposedHeaders("X-Pagination");
options.AllowAnyOrigin();
options.AllowAnyMethod();
});
因此,当您从客户端进行HTTP调用时,您将能够看到您的头