我有一个基于ASP.net Core 2的API(使用Windows身份验证),还有一个Angular前端页面。我现在想要配置跨域资源共享(CORS),以便从Angular单页应用(SPA)中查询我的后端服务,但是由于IIS服务器缺少身份验证信息,预检请求被拒绝,导致我目前无法实现该功能。
错误信息:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXXXXX' is therefore not allowed access. The response had HTTP status code 401.
代码前端:
//MY HEADER
private headers = new Headers({
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials':'true',
'Access-Control-Allow-Origin':'true'
});
//REQUEST
let options = new RequestOptions({headers:this.headers, withCredentials:true});
return this.http.get(this.tasksUrl,options).map(res=>res.json());
代码后端:(Startup.cs)
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("http://theURLofTheFront:8080" )
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors("AllowSpecificOrigin");
app.UseMvc();
}
我尝试了这个:CORS 预检请求返回 HTTP 401(需要身份验证)并使用 Windows 身份验证,并在 IIS 中添加自定义标头以指定“Access-control-allow-origin”,但对我不起作用。
这个对我也没用:https://blogs.msdn.microsoft.com/friis/2017/11/24/putting-it-all-together-cors-tutorial/。
我无法删除默认的授权规则。
提前感谢您。