我已经检查了其他几个与此有关的线程,但仍然无法弄清楚这一点。我希望允许任何来源、标头、方法等访问我的 .NET Core 2.2 API。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, IDistributedCache cache)
{
app.UseCors(builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
我确保首先在ConfigureServices 和Configure 中调用CORS 方法。我在本地和服务器上对此进行测试,并在 Chrome 中得到相同的错误。
Access to XMLHttpRequest at 'https://xxxxx.azurewebsites.net/api/Employees/getCurrentEmployee' from origin 'https://xxxxxxxxxx.azurewebsites.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
在ConfigureServices方法中
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.SetIsOriginAllowed((host) => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
在配置方法中
app.UseCors("CorsPolicy");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)