Cors问题与net.core和angular4应用程序 - 适用于azurewebsites.net域而非自定义

时间:2018-03-30 02:40:47

标签: c# angular asp.net-core cors

我在azurewebsites.net域名托管开发环境 Webapp在webapp.azurewebsites.net上 Api在api.azurewebsites.net上

此方案正常,这意味着应用客户端和api服务器端正确设置(cors)。

问题出现在我的实时场景中,我安装了自定义域和https证书: Webapp在www.webapp.com上 Api在api.webapp.com上

我检查了三次构建正确替换了cors origin set(web.config)的值,并且所有值都是正确的,但我仍然在实时自定义域上收到错误。

错误:

  

无法加载https://api.webapp.com/api/connect/token:响应   预检请求没有通过访问控制检查:否   '访问控制允许来源'标题出现在请求的上   资源。起源' http://www.webapp.com'因此是不允许的   访问。

我当然没有问题通过邮递员访问api。

Api startup.cs:

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.WithOrigins(Configuration.GetSection("HostSettings:WebAppClientUrl").Value.ToString())
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());
            });

            ConfigureDependencyInjection(services);

            // Register the Swagger generator, defining one or more Swagger documents
            services.AddSwaggerGen(x =>
            {
                x.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });

                // UseFullTypeNameInSchemaIds replacement for .NET Core
                x.CustomSchemaIds(y => y.FullName);

                //Set the comments path for the swagger json and ui.
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath = Path.Combine(basePath, "Api.xml");
                x.IncludeXmlComments(xmlPath);
            });

            // Add framework services.
            services
                .AddMvc()
                .AddFluentValidation(x => x.RegisterValidatorsFromAssemblyContaining<CreateViewModelValidator>());

            services.AddMvcCore()
                .AddJsonFormatters(x => x.Converters.Add(new StringEnumConverter()));

            // Add di framework
            var container = new Container(new DependencyInjectionRegistry());
            container.Populate(services);

            services.AddAutoMapper(cfg => cfg.ConstructServicesUsing(container.GetInstance));
            Mapper.AssertConfigurationIsValid();

            return container.GetInstance<IServiceProvider>();
        }

同样在configure方法中:

    app.UseCors("CorsPolicy");

appsesttings.json: {

  "HostSettings": {
    "WebAppClientUrl": "https://www.webapp.com"
  }
}

1 个答案:

答案 0 :(得分:0)

我不得不去Azure并在那里的web api上设置CORS。