【发布时间】:2017-10-14 14:24:30
【问题描述】:
我正在尝试设置 2 个 CORS 策略。一个作为 api 默认值,另一个在 Controllers 上使用,因为我需要它们。我想这样做的原因是因为我有一个端点,它接收一个带有电子邮件信息的对象并发送一封电子邮件(与我网页上的联系我框一起使用)并让它只接受来自我的域的请求。
我的startup.cs文件sn-p:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("Example",
builder => builder.WithOrigins("http://www.example.com"));
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin());
});
services.AddMvc();
//other configure stuff
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors(builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.WithOrigins("AllowAll");
});
app.UseMvcWithDefaultRoute();
}
我的emailcontroller.cs 文件:
using System.Threading.Tasks;
using MyAPI.Models;
using MyAPI.Services;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
namespace MyAPI.Controllers
{
[Produces("application/json")]
[Route("api/Email")]
[EnableCors("Example")]
public class EmailController : Controller
{
private readonly IEmailSender _emailSender;
public EmailController(IEmailSender emailSender)
{
_emailSender = emailSender;
}
[HttpPost]
public async Task Post([FromBody] Email email)
{
await _emailSender.SendEmailAsync(email);
}
}
}
用于发送电子邮件的Javascript:
function sendEmail(email)
{
var urlToApi = "http://<ipToApi>:5000/api";
$.ajax({
method: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(email),
url: urlToApi + "/email/",
success: function(data) {
console.log(data);
console.log('probably sent');
},
error: function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
alert("There was like, an error doing that");
}
});
}
这是我试图从http://www.example.com发送的内容
XMLHttpRequest cannot load http://<ipToApi>:5000/api/email/.
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://www.example.com' is therefore not allowed access.
编辑
这行得通:
services.AddCors(options =>
{
options.AddPolicy("Example",
builder => builder.WithOrigins("http://www.example.com")
.AllowAnyHeader()
.AllowAnyMethod());
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
【问题讨论】:
-
您将原点设置为“AllowAll”
-
@Mardoxx 我认为这只会设置默认值。如果我把所有这些都注释掉,我可以在我的所有控制器上使用 [EnableCors("mypolicy")] 吗?
-
你应该使用
app.UseCors("AllowAll"); -
您也不允许任何方法/标题。 CORS 规范说,如果任何检查未通过,则不要设置任何标头(至少 ASPNET Core 是这样解释它的!)所以这就是为什么我相信你会得到通用的
...-Allow-Origin not present错误。如果您将AllowAnyHeader和AllowAnyMethod添加到这两个策略中,它可能会按预期工作。 -
@Mardoxx 谢谢!如果您将其发布为答案,我会接受它
标签: javascript c# .net asp.net-core cors