首页 > 解决方案 > .NET Core 3.1 Cors 问题

问题描述

我已遵循 Microsoft 文章中列出的指南:在 ASP.NET Core 中启用跨域请求 (CORS),但我仍然无法从本地 vue 网站或 PostMan 访问 API。有什么建议么?

这是 AllowedHosts 中定义的内容:

"AllowedHosts": "http://localhost;http://localhost:8080"

这是启动类:

using App.Core.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace App.Core
{
    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.AddDbContext<ImportContext>((builder) =>
            {
                builder.UseSqlServer(Configuration.GetConnectionString("ParsingAppDb"));
            });
            services.AddDbContext<CodeAdminContext>((builder) =>
            {
                builder.UseSqlServer(Configuration.GetConnectionString("ParsingAppDb"));
            });
            services.AddScoped(typeof(IImportContext), typeof(ImportContext));
            services.AddScoped(typeof(ICodeAdminContext), typeof(CodeAdminContext));
            services.AddTransient(typeof(Logic.IImporter), typeof(Logic.Importer));
            services.AddTransient(typeof(Logic.I2964Procssor), typeof(Logic.Processor_2964));
            services.AddTransient(typeof(Logic.I2965Procssor), typeof(Logic.Processor_2965));

            var allowedHosts = Configuration.GetValue(typeof(string), "AllowedHosts") as string;
            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                    {
                        if (allowedHosts == null || allowedHosts == "*")
                        {
                            builder.AllowAnyOrigin()
                                .AllowAnyMethod()
                                .AllowAnyHeader();
                            return;
                        }
                        string[] hosts;
                        if (allowedHosts.Contains(';'))
                            hosts = allowedHosts.Split(';');
                        else
                        {
                            hosts = new string[1];
                            hosts[0] = allowedHosts;
                        }
                        builder.WithOrigins(hosts)
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials();
                    });
            }); services.AddControllers();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

即使使用提琴手也没有任何帮助,只是被拒绝的电话。相反,如果我将它设置为任何来源,我可以让 PostMan 工作,但 vue 网站然后返回没有设置任何 Accept-Control。

更新:我已经安装了 Microsoft.AspNetCore.Cors 包。

标签: c#asp.net-core.net-coreasp.net-core-webapi

解决方案


我发现了问题。它是配置值:

"AllowedHosts": "http://localhost;http://localhost:8080"

显然,此值以另一种方式使用,创建一个单独的 Cors 部分并将允许的 cors 主机放置在那里,然后将 AllowedHosts 值更改回 * 修复了该问题。


推荐阅读