我正在尝试在我的ASP上启用跨起源资源共享。NET核心Web API,但我卡住了。

EnableCors属性接受字符串类型的policyName作为参数:

// Summary:
//     Creates a new instance of the Microsoft.AspNetCore.Cors.Core.EnableCorsAttribute.
//
// Parameters:
//   policyName:
//     The name of the policy to be applied.
public EnableCorsAttribute(string policyName);

policyName是什么意思,如何在ASP上配置CORS。NET核心Web API?


当前回答

在我设法在最琐碎的CORS问题上浪费了两个小时后,一些故障排除技巧:

If you see CORS policy execution failed logged... Don't assume that your CORS policy is not executing properly. In fact, the CORS middleware works, and your policy is executing properly. The only thing this badly worded message means is that the request's origin doesn't match any of the allowed origins (see source), i.e. the request is disallowed. The origin check (as of ASP.NET Core 5.0) happens in a very simple way... i.e. case-sensitive ordinal string comparison (see source) between the strings you provided via WithOrigins() and what exists in HttpContext.Request.Headers[Origin]. CORS can fail if you set an allowed origin with a trailing slash /, or if it contains uppercase letters. (In my case I did in fact accidentally copy the host with a trailing slash.)

其他回答

步骤1:我们需要微软. aspnetcore . cors包在我们的项目。安装请进入“Tools -> NuGet Package Manager -> Manage NuGet Packages For Solution”。搜索“Microsoft.AspNetCore.Cors”,安装软件包。

步骤2:我们需要将CORS注入到容器中,以便应用程序可以使用它。在Startup.cs类中,让我们转到ConfigureServices方法并注册CORS。

因此,在我们的服务器应用程序中,让我们进入Controllers -> homeconcontroller .cs,并将EnableCors装饰器添加到Index方法(或您特定的控制器和动作):

更多详情请点击这里

得到这个工作与。net Core 3.1如下:

在ConfigureServices()方法中:

 public void ConfigureServices(IServiceCollection services)
  {
   ...
   services.AddCors();
   ...
  }

在Configure()方法中:

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
   ...
   app.UseCors(builder =>
          builder.AllowAnyOrigin()
          .AllowAnyHeader()
          .AllowAnyMethod()
        );
   ...
  }

上面提到的所有解决办法都可能有效,也可能无效,在大多数情况下都行不通。我已经给出了答案

目前我正在研究Angular和Web API(.net Core),遇到了下面解释的CORS问题

上面提供的解决方案总是有效的。对于“OPTIONS”请求,真的有必要启用“匿名身份验证”。使用这里提到的解决方案,您不必执行上面提到的所有步骤,例如IIS设置。

不管怎样,有人把我上面的帖子标记为这篇帖子的副本,但我可以看到这篇帖子只是为了在ASP.net Core中启用CORS,但我的帖子与在ASP.net Core和Angular中启用和实现CORS有关。

注意“/”在最后-会阻塞CORS的原点

builder.WithOrigins("http://example.com/","http://localhost:55233/");

将阻止

use

builder.WithOrigins("http://example.com","http://localhost:55233"); 

适用于。net Core 1和。net Core 2

如果使用.Net-Core 1.1

不幸的是,在这个特定的情况下,文件非常混乱。所以我要让它变得非常简单:

将Microsoft.AspNetCore.Cors nuget包添加到项目中 在ConfigureServices方法中添加services.AddCors(); 在Configure方法中,在调用app.UseMvc()和app.UseStaticFiles()之前,添加: app.UseCors(生成器=>生成器 .AllowAnyOrigin () .AllowAnyMethod () .AllowAnyHeader () .AllowCredentials ());

就是这样。每个客户端都可以访问您的ASP。NET核心网站/API。


如果使用。net - core 2.0

Add Microsoft.AspNetCore.Cors nuget package to your project in ConfigureServices method, before calling services.AddMvc(), add: services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); (Important) In Configure method, before calling app.UseMvc(), add app.UseCors("AllowAll"); "AllowAll" is the policy name which we need to mention in app.UseCors. It could be any name.