Angular ASPWebApi对飞行前请求的响应不';无法通过访问控制检查

Angular ASPWebApi对飞行前请求的响应不';无法通过访问控制检查,angular,asp.net-web-api,Angular,Asp.net Web Api,当我的angular应用程序尝试访问ASP WebApi应用程序时,我遇到了一个问题,每个应用程序都位于不同的服务器中。 当我的angular应用程序尝试访问时,我收到一条消息:“从源”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许源”标头 根据我找到的文档,我尝试以不同的方式(全局、控制操作)启用CORS,但问题仍然存在 当我运行angular应用程序时,我运行:$ng serve--host 0.0.0.0--port 4200--disable

当我的angular应用程序尝试访问ASP WebApi应用程序时,我遇到了一个问题,每个应用程序都位于不同的服务器中。 当我的angular应用程序尝试访问时,我收到一条消息:“从源”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许源”标头

根据我找到的文档,我尝试以不同的方式(全局、控制操作)启用CORS,但问题仍然存在

当我运行angular应用程序时,我运行:$ng serve--host 0.0.0.0--port 4200--disable host check 如果我运行时没有“禁用主机检查”,则应用程序不会运行

这是我的WebApi.config.cs:

public static void Register(HttpConfiguration config)
    {
        // Enable CORS: le estamos permitiendo a la direccion:4200 consumir la aplicacion con todo y sus metodos
        // config.EnableCors(new EnableCorsAttribute("http://ubex:4200", headers:"*", methods:"*"));
        // config.EnableCors(new EnableCorsAttribute("http://192.168.100.152:4200", headers: "*", methods: "*"));


        EnableCorsAttribute cors = new EnableCorsAttribute("http://192.168.100.152:4200", "*", "GET,POST");
        config.EnableCors(cors);


        // Web API configuration and services

        //Configuración para verificar la seguridad del CORS
        //var corsAttr = new EnableCorsAttribute("*", "*", "*");
        //config.EnableCors(corsAttr);
如您所见,我已经尝试了几种方法,这是添加到我的web.config的标题:

<httpProtocol>
      <customHeaders>

          <add name="Access-Control-Allow-Origin" value="*" />
          <!--<add name="Access-Control-Allow-Headers" value="Content-Type, Origin, X-Requested-With, Accept" />-->
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
          <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
          <add name="Access-Control-Allow-Credentials" value="true" />
        <!--
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />
        -->
      </customHeaders>
</httpProtocol>
拜托,我需要知道,我是否错过了什么,我希望任何人都能帮助我


致以最诚挚的问候

我猜您的问题主要是在您的开发环境中,API托管在IIS中,您使用基于节点的lite服务器的
npm run
ng serve
运行Angular应用程序

为了解决这个问题,您不需要添加CORS头,这意味着有一种更简单的方法来处理这个问题:Angular的代理配置(实际上是webpack的)。设置代理config.json,作为API请求的代理。通常,它将包含一个JSON,如下所示:

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false
  }
}
除此之外,还有很多关于这个话题的博客。以下是一些:

  • 修复Angular 8中的CORS:使用Alex Ssanya提供的CLI代理配置
  • 棱角的 — Bhargav Bachina如何代理后端服务器(非常详细,但请注意,这可能需要中等订阅。)
如果您在实际生产中遇到这个问题,这里还有一个例子。Thanh Nguyen Tien的“Angular 2+不支持prod build中的代理,以及如何使用API.NET RESTful API解决它”

感谢Krishnan

我创建了配置代理文件:

{
"/api/*": {
"target": "http://192.168.100.151:80"
, "secure": false
, "changeOrigin": true
, "logLevel": "debug"
, "pathRewrite": {
  "^/api": "http://192.168.100.151:80/api"
}
} }

参考angular.json中的代理配置:

"serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "app10crudapi:build"
        , "proxyConfig": "proxy.conf.json"
      },
(以防万一)我允许在WebApi服务器中输入连接:

netsh http add urlacl url=http://192.168.100.151:80/ user=everyone
netsh advfirewall firewall add rule name="IISWeb123" dir=in protocol=tcp localport=80 
profile=private remoteip=localsubnet action=allow
这是用于本地子网的

我在IIS配置中检查了Access Control Allow Origin,因为它有多个值(我不知道为什么),但它现在可以工作了

在我的WebApiConfig.cs上:

 public static void Register(HttpConfiguration config)
    {
        EnableCorsAttribute cors = new EnableCorsAttribute("http://192.168.100.152:4200", "*", "GET,POST");
        config.EnableCors(cors);
netsh http add urlacl url=http://192.168.100.151:80/ user=everyone
netsh advfirewall firewall add rule name="IISWeb123" dir=in protocol=tcp localport=80 
profile=private remoteip=localsubnet action=allow
 public static void Register(HttpConfiguration config)
    {
        EnableCorsAttribute cors = new EnableCorsAttribute("http://192.168.100.152:4200", "*", "GET,POST");
        config.EnableCors(cors);