简体   繁体   English

IIS 上的 Net Core Cors 和 Angular 应用程序

[英]Net Core Cors and Angular application on IIS

I'm having some troubles setting up the correct Cors settings on my application.我在我的应用程序上设置正确的 Cors 设置时遇到了一些麻烦。 NetCore application with Angular6 hosted on IIS , the angular app is outside the .Net project and compiled and inserted inside the wwwroot when publishing. NetCore与应用Angular6托管在IIS中, angular应用程序超出了.Net项目,编译和插入内部wwwroot发布时。

Now the problem is that when I'm coding the angular part, I'd like to call directly the release server to test some functionality.现在的问题是,当我对angular部分进行编码时,我想直接调用发布服务器来测试一些功能。 I tried any kind of approach to have this work out but it seems like I'm always hitting a problem with the Cors setup but only for the POST calls, GET works fine.我尝试了任何一种方法来解决这个问题,但似乎我总是在 Cors 设置方面遇到问题,但仅对于POST调用, GET工作正常。 So here is my startup code:所以这是我的启动代码:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ILoggerManager, LoggerManager>();
    services.AddSingleton<IDbContext, MongoDbContext>();
    services.AddSingleton<IIdGenerator, IdGenerator>();
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        c.AddSecurityDefinition("Bearer", new ApiKeyScheme(){In = "header",Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey"});
        c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
        {
            {"Bearer", Enumerable.Empty<string>()}
        });
    });
    services.AddCustomRepositories();
    services.AddCustomServices();
    services.AddJwtService();

    //Add cors
    services.AddCors();

    // Add framework services.
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    services.Configure<IISOptions>(options =>
    {
        options.ForwardClientCertificate = false;
    });
    services.Configure<Settings>(Configuration.GetSection("SystemSettings"));
}

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

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

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

    app.UseDefaultFiles();

    // this will serve js, css, images etc.
    app.UseStaticFiles();
    app.Use(async (context, next) =>
    {
        if (context.Request.Path.HasValue && 
            !context.Request.Path.Value.StartsWith("/api/") &&
            !context.Request.Path.Value.StartsWith("/swagger"))
        {
            context.Response.ContentType = "text/html";
            await context.Response.SendFileAsync(
                env.ContentRootFileProvider.GetFileInfo("wwwroot/index.html")
            );
            return;
        }
        await next();
    });

    //app.UseHttpsRedirection();
    app.UseSwagger();
    app.UseAuthentication();
    app.UseMiddleware(typeof(ErrorHandlingMiddleware));

    app.UseMvc();  

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        c.DocExpansion(DocExpansion.None);
    });
}

Since I want this enabled only for development purpose I'd like to enable it globally.由于我希望仅出于开发目的启用此功能,因此我想在全球范围内启用它。

i think you need to set the origin manually 我认为您需要手动设置原点

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

as shown in here 这里所示

将 cors 添加到启动 asp.net core web API

This's the necessary part to add cors in your web API.这是在 Web API 中添加 cors 的必要部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM