【问题标题】:Getting CORS To Work With Nancy让 CORS 与 Nancy 合作
【发布时间】:2014-11-28 02:05:28
【问题描述】:

我正在尝试让所有类型的请求与 Nancy 和 CORS 一起工作。目前我在请求的末尾添加了一个管道:

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => ctx.Response
            .WithHeader("Access-Control-Allow-Origin", "http://localhost:57515")
            .WithHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS")
            .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type")
            .WithHeader("Allow", "POST, GET, DELETE, PUT, OPTIONS"))

选项请求返回状态码 200,这让我相信它执行得很好,但是对于 OPTIONS 以外的任何类型的请求,它都会失败,并显示 405 Method Not Allowed。为了让它工作,我还需要在客户端或服务器端做些什么吗?

我使用的客户端库是主干。

提前致谢。

【问题讨论】:

    标签: c# asp.net api cors nancy


    【解决方案1】:

    在 .NET Core 上,我能够在 Configure 方法中使用以下代码通过 CORS:

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
    
            // Shows UseCors with CorsPolicyBuilder.
            app.UseCors(builder =>
               builder.WithOrigins(new[] { "http://localhost:4200" }).AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowCredentials()
               );
    
            app.UseOwin(a => a.UseNancy(b => b.Bootstrapper = new NancyBootstrap(this.Container)));
    
        }
    

    【讨论】:

      【解决方案2】:

      我认为您不需要将 OPTIONS 指定为允许的 CORS 方法。我从来没有见过那个设置,我自己也从来没有设置过。浏览器不会抱怨。

      另外,我的设置和你类似:

      public abstract class MyModule : NancyModule
          protected MyModule(bool hasRazorView = true)
              After.AddItemToEndOfPipeline((ctx) => ctx.Response
                  .WithHeader("Access-Control-Allow-Origin", "*")
                  .WithHeader("Access-Control-Allow-Methods", "POST,GET")
                  .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"));
              . . . .
          }
      }
      

      在我的情况下,CORS 在我的 GET 和 POST 上发送,但不是在 OPTIONS 上发送。我用Options["/"] = route => new Response() 覆盖了默认的选项处理。这让管道的其余部分着火了。

      【讨论】:

      • 这应该是答案。这对我很有帮助,非常感谢。
      【解决方案3】:

      我发现在 IIS 重写器规则中处理 CORS 标头更好,这是示例重写 webconfig 部分,它可以做到这一点:

      <rewrite>
        <outboundRules>
          <rule name="Set Access Control Allow Origin Header" preCondition="Origin header">
            <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern="(.*)" />
            <action type="Rewrite" value="{HTTP_ORIGIN}" />
          </rule>
          <rule name="Set Access Control Allow Headers Header" preCondition="Origin header">
            <match serverVariable="RESPONSE_Access-Control-Allow-Headers" pattern="(.*)" />
            <action type="Rewrite" value="Content-Type" />
          </rule>
          <rule name="Set Access Control Allow Methods Header" preCondition="Origin header">
            <match serverVariable="RESPONSE_Access-Control-Allow-Method" pattern="(.*)" />
            <action type="Rewrite" value="POST,GET,OPTIONS" />
          </rule>
          <rule name="Set Access Control Allow Credentials Header" preCondition="Origin header">
            <match serverVariable="RESPONSE_Access-Control-Allow-Credentials" pattern="(.*)" />
            <action type="Rewrite" value="true" />
          </rule>
          <preConditions>
            <preCondition name="Origin header" logicalGrouping="MatchAny">
              <add input="{HTTP_ORIGIN}" pattern="(http://localhost$)" />
              <add input="{HTTP_ORIGIN}" pattern="(http://.*\.YOURDOMAIN\.com$)" />
            </preCondition>
          </preConditions>
        </outboundRules>
      </rewrite>
      

      记得安装IIS重写模块:http://www.iis.net/downloads/microsoft/url-rewrite

      【讨论】:

      • 我认为这不是真正的答案,因为它取决于托管平台,如果您使用 NancyFX 和 OWIN,您可以在各种平台上托管。这个问题似乎也没有提到 IIS。 :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2011-01-22
      • 2011-06-30
      • 2014-03-29
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多