C# 测试本地服务器时CORS存在的问题
我有一个问题: 访问位于的XMLHttpRequest 'https://localhost:5001/api/user/authenticate"从源头上说, 'http://localhost:4200'已被CORS策略阻止:响应 飞行前请求未通过访问控制检查:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源 我一直在尝试几种解决方法,例如使用chrome插件CORS unblock,但到目前为止没有成功。使用插件时,我收到一个不同的错误: 访问位于的XMLHttpRequest 'https://localhost:5001/api/user/authenticate"从源头上说, 'http://localhost:4200'已被CORS策略阻止:响应 飞行前请求未通过访问控制检查:它没有 HTTP ok状态 在my Startup.CS中,我有以下内容:C# 测试本地服务器时CORS存在的问题,c#,angular,asp.net-core,.net-core,C#,Angular,Asp.net Core,.net Core,我有一个问题: 访问位于的XMLHttpRequest 'https://localhost:5001/api/user/authenticate"从源头上说, 'http://localhost:4200'已被CORS策略阻止:响应 飞行前请求未通过访问控制检查:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源 我一直在尝试几种解决方法,例如使用chrome插件CORS unblock,但到目前为止没有成功。使用插件时,我收到一个不同的错误: 访问位
//ConfigureServices
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.WithOrigins("https://localhost:5001", "http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
//Configure
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseCors("MyPolicy");
app.UseMvc();
前端:
return this.http.post<ApiResult>(`${environment.apiUrl}/user/authenticate`, { username, password })
返回this.http.post(`${environment.apirl}/user/authenticate`,{username,password})
我正在使用:
- dotnetcore3.1
- 角度8