Skip to content

Commit

Permalink
feat: add possibility to configure cors
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Jan 29, 2025
1 parent fc3413f commit b8bd780
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Applications/SseServer/src/SseServer/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Configuration
[Required]
public AuthenticationConfiguration Authentication { get; set; } = new();

public CorsConfiguration Cors { get; set; } = new();

[Required]
public InfrastructureConfiguration Infrastructure { get; set; } = new();

Expand All @@ -20,6 +22,12 @@ public class AuthenticationConfiguration
public string JwtSigningCertificate { get; set; } = "";
}

public class CorsConfiguration
{
public string AllowedOrigins { get; set; } = "";
public string ExposedHeaders { get; set; } = "";
}

public class InfrastructureConfiguration
{
[Required]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public static void AddCustomAspNetCore(this IServiceCollection services,
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
});

services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder
.WithOrigins(configuration.Cors.AllowedOrigins.Split(";"))
.WithExposedHeaders(configuration.Cors.ExposedHeaders.Split(";"))
.AllowAnyHeader()
.AllowAnyMethod();
});
});

services.AddAuthentication().AddJwtBearer("default", options =>
{
var privateKeyBytes = Convert.FromBase64String(configuration.Authentication.JwtSigningCertificate);
Expand Down
2 changes: 2 additions & 0 deletions Applications/SseServer/src/SseServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ static void Configure(WebApplication app)
.AddCustomHeader("X-Frame-Options", "Deny")
);

app.UseCors();

app.UseAuthentication().UseAuthorization();

app.MapControllers();
Expand Down

0 comments on commit b8bd780

Please sign in to comment.