Asp.net core asp.net核心CORS没有像我预期的那样工作

Asp.net core asp.net核心CORS没有像我预期的那样工作,asp.net-core,asp.net-identity,Asp.net Core,Asp.net Identity,我有一个节点应用程序,它托管在localhost:9000上运行(React with Express)。我正在向我的ASP.NET Core 2.0 web项目发布一篇axios REST文章,内容如下: http://localhost:50494/rest/sessions 获得工作,但发布没有。在我的Startup.cs文件中,我相信我已经设置了asp.net核心端点上允许的所有来源和方法,但我仍然得到了我认为是CORS not setup错误 http://localhost:504

我有一个节点应用程序,它托管在localhost:9000上运行(React with Express)。我正在向我的ASP.NET Core 2.0 web项目发布一篇axios REST文章,内容如下:

http://localhost:50494/rest/sessions
获得工作,但发布没有。在我的Startup.cs文件中,我相信我已经设置了asp.net核心端点上允许的所有来源和方法,但我仍然得到了我认为是CORS not setup错误

http://localhost:50494/rest/sessions/6184: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
以下是我在asp.net端的设置:

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

为什么会出现此错误?

您是否也可以显示您的
ConfigureServices
方法?我现在正在显示我的ConfigureServices。这让我想到也许addCors应该在addMvc之后出现,所以我将它们颠倒过来,但这并没有帮助。还是同一个错误:(@Alexyn不确定为什么我的不起作用,但当我使用Rick Strahl的时,它起作用了,我得到了正确的标题。我想这是因为你没有添加
AllowAnyHeader()
。此外,我注意到你的代码中还有一个问题,虽然你没有要求这样做,但我认为对此进行评论是有意义的:)在
Configure
方法的开头,使用
usedeveloperceptionpage
UseExceptionHandler
middleware移动所有if条件。否则,您将不会在开发环境中看到包含抛出异常详细信息的异常页面,并且如果MVC中间件中发生错误,用户将不会被重定向到错误页面。
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();
        services.AddMvc();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {


        app.UseStaticFiles();

        app.UseCors(builder =>
                    builder.AllowAnyOrigin().AllowAnyMethod());

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }