【发布时间】:2019-04-11 02:33:33
【问题描述】:
我无法让 cors 工作 angular .net core 2.1 我收到此错误:
从源“https://page”访问“https://dev...SaveAPP”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:预检请求不允许重定向。
这是我的 startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.WithOrigins("http://localhost:4200", "https://e.corpintra.net", "https://specit-dtna-dev.e.corpintra.net", "https://specit-dtna.e.corpintra.net", "https://specit-dtna-test.e.corpintra.net")
.AllowAnyMethod()
.WithExposedHeaders("content-disposition")
.AllowAnyHeader()
.AllowCredentials()
.SetPreflightMaxAge(TimeSpan.FromSeconds(3600)));
});
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAll"));
});
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (env.IsProduction() || env.IsStaging())
{
app.UseExceptionHandler("/Error");
}
// app.UseCorsMiddleware();
app.UseCors("AllowAll");
// app.UseCors(builder =>
// builder.WithOrigins("http://localhost:4200", "http://localhost:5000")
//.AllowAnyOrigin()
//.AllowAnyHeader()
//.AllowAnyMethod());
app.UseMvc();
}
从角度我正在使用拦截器
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
withCredentials: true
});
return next.handle(request);
}
我的 api 同时启用了 windows 身份验证和匿名身份验证。
这是在 Intranet 上。
【问题讨论】: