generated from fossapps/Micro.Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from fossapps/cors
feat(cors): add ability to setup cors
- Loading branch information
Showing
5 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters