我应该如何设置 net core api cors?
How should I set net core api cors?
我正在为自己编写一个非官方的 Twitter api。然后,我使用浏览器中的控制台屏幕通过以下方法向此 api 发送一个 get。
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
这样
httpGet(https://localhost:44311/WeatherForecast/alienationxs/true);
问题是,当我通过 www.google.com 执行此操作时,没问题,并且 json 数据到达了我。但是当我通过 twitter.com 执行此操作时,出现以下错误。
通过google.com
我在 api
上的 cors 设置
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
options.AddDefaultPolicy(builder =>
builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin())); ;
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "twitterAPI", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "twitterAPI v1"));
}
app.UseRouting();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我只想通过 twitter.com 到达我的 api 就像 google.com.
推特 API 不支持 CORS。
- 首先,让我们把苍蝇和炸肉排分开。
跨源资源共享 (CORS) - 是一个单独的安全层。
内容安全策略 (CSP) - 是一个单独的安全层,它在 CORS 之前应用。通过 CSP 后,如果最后一个被破坏,你将面临 CORS。
正如您从错误消息“...中看到的那样,因为它违反了以下内容安全策略指令...”,您面临 CSP 违规,因此您的 CORS 设置有没有意思。
- 怎么回事。
您进入 twitter.com
网页并尝试代表 Twitter 网页向 localhost:44311
执行连接请求。但受 CSP 保护的推特网页禁止此类请求:
注意 'connect-src' 指令,它管理 XMLHttpRequest()。
www.google.com
网页没有CSP,因此您代表google请求成功。
我正在为自己编写一个非官方的 Twitter api。然后,我使用浏览器中的控制台屏幕通过以下方法向此 api 发送一个 get。
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
这样
httpGet(https://localhost:44311/WeatherForecast/alienationxs/true);
问题是,当我通过 www.google.com 执行此操作时,没问题,并且 json 数据到达了我。但是当我通过 twitter.com 执行此操作时,出现以下错误。
通过google.com
我在 api
上的 cors 设置public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
options.AddDefaultPolicy(builder =>
builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin())); ;
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "twitterAPI", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "twitterAPI v1"));
}
app.UseRouting();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我只想通过 twitter.com 到达我的 api 就像 google.com.
推特 API 不支持 CORS。
- 首先,让我们把苍蝇和炸肉排分开。
跨源资源共享 (CORS) - 是一个单独的安全层。
内容安全策略 (CSP) - 是一个单独的安全层,它在 CORS 之前应用。通过 CSP 后,如果最后一个被破坏,你将面临 CORS。
正如您从错误消息“...中看到的那样,因为它违反了以下内容安全策略指令...”,您面临 CSP 违规,因此您的 CORS 设置有没有意思。
- 怎么回事。
您进入 twitter.com
网页并尝试代表 Twitter 网页向 localhost:44311
执行连接请求。但受 CSP 保护的推特网页禁止此类请求:
www.google.com
网页没有CSP,因此您代表google请求成功。