Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 测试本地服务器时CORS存在的问题_C#_Angular_Asp.net Core_.net Core - Fatal编程技术网

C# 测试本地服务器时CORS存在的问题

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,但到目前为止没有成功。使用插件时,我收到一个不同的错误: 访问位

我有一个问题:

访问位于的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中,我有以下内容:

//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

您能发布整个启动过程吗?请输入您的代码,以显示您所做的研究。这是关于堆栈溢出的最常见错误之一,您将被否决,而不会显示您所做的研究以及您已经研究的问题。您是否收到访问控制允许源标题?@Crowcoder是的,我得到的回答是:访问控制允许源:localhost:4200,但只有当我激活CORS解锁插件时