Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ad2ea48592 | |||
| bf36bb6bbc | |||
| 0a9a459892 | |||
| 00d43c8f10 | |||
| f5a69de0a0 | |||
| 40a5f94941 |
@@ -54,12 +54,20 @@ public static class Api
|
|||||||
|
|
||||||
public static IServiceCollection AddLiteCharmsWebSecurity(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddLiteCharmsWebSecurity(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var certificate = X509CertificateLoader.LoadPkcs12(Convert.FromBase64String(configuration["DataProtection:Certificate"]!), configuration["DataProtection:Password"]);
|
var certString = configuration["DataProtection:Certificate"] ?? configuration["DataProtection__Certificate"];
|
||||||
|
var certPassword = configuration["DataProtection:Password"] ?? configuration["DataProtection__Password"];
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(certString))
|
||||||
|
throw new InvalidOperationException("Data Protection Certificate configuration is missing.");
|
||||||
|
|
||||||
|
var certificate = X509CertificateLoader.LoadPkcs12(Convert.FromBase64String(certString), certPassword);
|
||||||
|
|
||||||
services.AddDataProtection().PersistKeysToDbContext<DataProtectionDbContext>()
|
services.AddDataProtection().PersistKeysToDbContext<DataProtectionDbContext>()
|
||||||
.ProtectKeysWithCertificate(certificate)
|
.ProtectKeysWithCertificate(certificate)
|
||||||
.SetApplicationName("LiteCharmsApp");
|
.SetApplicationName("LiteCharmsApp");
|
||||||
|
|
||||||
|
services.Configure<DataProtectionOptions>(options => options.ApplicationDiscriminator = "LiteCharmsApp");
|
||||||
|
|
||||||
services.ConfigureCookieOidcSameSiteSupport();
|
services.ConfigureCookieOidcSameSiteSupport();
|
||||||
|
|
||||||
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
||||||
@@ -137,8 +145,17 @@ public static class Api
|
|||||||
private static void CheckSameSite(HttpContext httpContext, CookieOptions options)
|
private static void CheckSameSite(HttpContext httpContext, CookieOptions options)
|
||||||
{
|
{
|
||||||
if (options.SameSite == SameSiteMode.None)
|
if (options.SameSite == SameSiteMode.None)
|
||||||
if (!httpContext.Request.IsHttps && httpContext.Request.Headers["X-Forwarded-Proto"] != "https")
|
{
|
||||||
options.SameSite = SameSiteMode.Unspecified;
|
bool isSecure = httpContext.Request.IsHttps;
|
||||||
|
|
||||||
|
if (!isSecure && httpContext.Request.Headers.TryGetValue("X-Forwarded-Proto", out var proto))
|
||||||
|
isSecure = string.Equals(proto, "https", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
if (!isSecure && httpContext.Request.Headers.TryGetValue("Forwarded", out var forwarded))
|
||||||
|
isSecure = forwarded.ToString().Contains("proto=https", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
if (!isSecure) options.SameSite = SameSiteMode.Unspecified;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
|||||||
Reference in New Issue
Block a user