Olu*_*emi 154 c# asp.net-core
我试图在我的ASP.NET Core Web API上启用跨源资源共享,但我被困住了.
该EnableCors属性接受policyName类型string为参数:
// 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);
Run Code Online (Sandbox Code Playgroud)
这policyName意味着什么以及如何在ASP.NET Core Web API上配置CORS?
Hen*_*ema 261
您必须在ConfigureServices方法中的应用程序启动时配置CORS策略:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
// ...
}
Run Code Online (Sandbox Code Playgroud)
将CorsPolicyBuilder在builder允许您配置的策略,以您的需求.您现在可以使用此名称将策略应用于控制器和操作:
[EnableCors("MyPolicy")]
Run Code Online (Sandbox Code Playgroud)
或者将其应用于每个请求:
public void Configure(IApplicationBuilder app)
{
app.UseCors("MyPolicy");
// ...
// This should always be called last to ensure that
// middleware is registered in the correct order.
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
Vah*_*iri 94
这是为了 .Net-Core 1.1
不幸的是,在这个具体案例中,文档非常混乱.所以我会让它变得简单:
Microsoft.AspNetCore.Corsnuget包添加到您的项目中ConfigureServices方法中,添加services.AddCors();在Configure方法调用之前app.UseMvc()和app.UseStaticFiles(),加:
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
Run Code Online (Sandbox Code Playgroud)而已.每个客户都可以访问您的ASP.NET核心网站/ API.
对于 .Net-Core 2.0
Microsoft.AspNetCore.Corsnuget包添加到您的项目中在ConfigureServices方法中,在调用之前services.AddMvc(),添加:
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
Run Code Online (Sandbox Code Playgroud)在Configure方法中,在调用之前app.UseMvc(),添加 app.UseCors("AllowAll");
AllowAll是我们需要在app.UserCors中提及的策略名称.它可以是任何名字.
Olu*_*emi 36
基于Henk的回答,我已经能够提出特定的域,我想要允许的方法以及我想要启用CORS的头:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
options.AddPolicy("AllowSpecific", p => p.WithOrigins("http://localhost:1233")
.WithMethods("GET")
.WithHeaders("name")));
services.AddMvc();
}
Run Code Online (Sandbox Code Playgroud)
用法:
[EnableCors("AllowSpecific")]
Run Code Online (Sandbox Code Playgroud)
tfa*_*tfa 31
与.NET Core 3.1一起使用,如下所示
UseCors代码放在app.UseRouting();和app.UseAuthentication();app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("CorsApi");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
Run Code Online (Sandbox Code Playgroud)
ConfigureServices方法中services.AddCors(options =>
{
options.AddPolicy("CorsApi",
builder => builder.WithOrigins("http://localhost:4200", "http://mywebsite.com")
.AllowAnyHeader()
.AllowAnyMethod());
});
Run Code Online (Sandbox Code Playgroud)
[EnableCors("CorsApi")]
[Route("api/[controller]")]
[ApiController]
public class BaseController : ControllerBase
Run Code Online (Sandbox Code Playgroud)
现在我所有的控制器都将继承BaseController并启用 CORS
Rez*_*abi 13
您可以通过三种方式启用 CORS:
使用命名策略启用 CORS:
public class Startup
{
readonly string CorsPolicy = "_corsPolicy";
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: CorsPolicy,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
// services.AddResponseCaching();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors(CorsPolicy);
// app.UseResponseCaching();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用UseResponseCaching时,必须在UseResponseCaching之前调用UseCors。
使用默认策略启用 CORS:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用端点启用 CORS
public class Startup
{
readonly string CorsPolicy = "_corsPolicy ";
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: CorsPolicy,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers()
.RequireCors(CorsPolicy)
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用属性启用 CORS
你有两个选择
Ros*_*sim 10
如果您在 IIS 上托管,一个可能的原因是您收到此消息是因为 IIS 正在阻止OPTIONS动词。因为这个,我花了将近一个小时:
一个迹象是您404在OPTIONS请求期间遇到错误。
要解决此问题,您需要明确告诉 IIS不要阻止OPTIONS请求。
转到请求过滤:
确保允许 OPTIONS:
或者,只需web.config使用以下设置创建一个:
<system.webServer>
<security>
<requestFiltering>
<verbs>
<remove verb="OPTIONS" />
<add verb="OPTIONS" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
你必须在 Startup.cs 类中配置
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
Run Code Online (Sandbox Code Playgroud)
特别是在使用SignalR的dotnet core 2.2中,您必须进行更改
.WithOrigins("http://localhost:3000") 要么
.SetIsOriginAllowed(isOriginAllowed: _ => true) //for all origins
而不是.AllowAnyOrigin()用.AllowCredentials()
https://trailheadtechnology.com/breaking-change-in-aspnetcore-2-2-for-signalr-and-cors/
https://github.com/aspnet/AspNetCore/issues/4483
小智 7
对于Web API(ASP.Net core 6.0)在Program.cs中只需在builder.Build()之前添加;
builder.Services.AddCors(p => p.AddPolicy("corsapp", builder =>
{
builder.WithOrigins("*").AllowAnyMethod().AllowAnyHeader();
}));
Run Code Online (Sandbox Code Playgroud)
还添加
app.UseCors("corsapp");
Run Code Online (Sandbox Code Playgroud)
小智 5
`
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.Configure<MvcOptions>(options => {
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAnyOrigin"));
});
}
Run Code Online (Sandbox Code Playgroud)
`
| 归档时间: |
|
| 查看次数: |
105675 次 |
| 最近记录: |