Skip to content

Commit

Permalink
Merge pull request #19 from fossapps/cors
Browse files Browse the repository at this point in the history
feat(cors): add ability to setup cors
  • Loading branch information
cyberhck authored Jul 28, 2020
2 parents ea1a953 + a2f9759 commit a2f0780
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Feature.Manager.Api/Configs/CorsConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace Feature.Manager.Api.Configs
{
public class CorsConfig
{
public IEnumerable<string> Origins { set; get; }
public IEnumerable<string> Headers { set; get; }
public bool AllowCredentials { set; get; }
public string PolicyToUse { set; get; }
}
}
6 changes: 5 additions & 1 deletion Feature.Manager.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Text.Json.Serialization;
using Feature.Manager.Api.Configs;
using Feature.Manager.Api.StartupExtensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace Feature.Manager.Api
{
Expand All @@ -30,15 +32,17 @@ public void ConfigureServices(IServiceCollection services)
});
services.ConfigureSwagger();
services.RegisterWorker();
services.SetupCors(Configuration.GetSection("CorsConfig").Get<CorsConfig>());
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<CorsConfig> corsConfig)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCorsPolicy(corsConfig.Value);
app.UseRouting();
app.UseAuthorization();
app.AddSwaggerWithUi();
Expand Down
1 change: 1 addition & 0 deletions Feature.Manager.Api/StartupExtensions/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class Configuration
public static void AddConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<DatabaseConfig>(configuration.GetSection("DatabaseConfig"));
services.Configure<CorsConfig>(configuration.GetSection("CorsConfig"));
}
}
}
41 changes: 41 additions & 0 deletions Feature.Manager.Api/StartupExtensions/Cors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Linq;
using Feature.Manager.Api.Configs;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Feature.Manager.Api.StartupExtensions
{
public static class Cors
{
public static void SetupCors(this IServiceCollection services, CorsConfig config)
{
services.AddCors(x =>
{
x.AddPolicy("development", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
x.AddPolicy("production", builder =>
{
builder.WithOrigins(config.Origins.ToArray())
.WithMethods(config.Headers.ToArray())
.AllowAnyMethod();
if (!config.AllowCredentials)
{
builder.DisallowCredentials();
return;
}
builder.AllowCredentials();
});
});
}

public static void UseCorsPolicy(this IApplicationBuilder app, CorsConfig config)
{
app.UseCors(config.PolicyToUse);
}
}
}
8 changes: 7 additions & 1 deletion Feature.Manager.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
"Database": "monitoring"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"CorsConfig": {
"Origins": ["*"],
"Headers": ["x-client-version"],
"AllowCredentials": false,
"PolicyToUse": "development"
}
}

0 comments on commit a2f0780

Please sign in to comment.