Csp: localhost

This commit is contained in:
pavelbannov 2023-08-18 17:01:32 +03:00
parent 44a1021614
commit 112d796b72
2 changed files with 19 additions and 3 deletions

View File

@ -32,6 +32,7 @@ public class Tenant : IMapFrom<DbTenant>
public const int DefaultTenant = -1;
public static readonly string HostName = Dns.GetHostName().ToLowerInvariant();
public const string LocalHost = "localhost";
private List<string> _domains;

View File

@ -47,6 +47,7 @@ public class CspSettingsHelper
private readonly TenantManager _tenantManager;
private readonly CoreSettings _coreSettings;
private readonly IDistributedCache _distributedCache;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
public CspSettingsHelper(
@ -55,6 +56,7 @@ public class CspSettingsHelper
TenantManager tenantManager,
CoreSettings coreSettings,
IDistributedCache distributedCache,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration)
{
_settingsManager = settingsManager;
@ -62,21 +64,34 @@ public class CspSettingsHelper
_tenantManager = tenantManager;
_coreSettings = coreSettings;
_distributedCache = distributedCache;
_httpContextAccessor = httpContextAccessor;
_configuration = configuration;
}
public async Task<string> Save(IEnumerable<string> domains)
{
var headerKey = GetKey(_tenantManager.GetCurrentTenant().GetTenantDomain(_coreSettings));
var tenant = _tenantManager.GetCurrentTenant();
var domain = tenant.GetTenantDomain(_coreSettings);
List<string> headerKeys = new()
{
GetKey(domain)
};
if (domain == Tenant.LocalHost && tenant.Alias == Tenant.LocalHost)
{
headerKeys.Add(GetKey(Tenant.HostName));
headerKeys.Add(GetKey(_httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString()));
}
var headerValue = CreateHeader(domains);
if (!string.IsNullOrEmpty(headerValue))
{
await _distributedCache.SetStringAsync(headerKey, headerValue);
await Parallel.ForEachAsync(headerKeys, async (headerKey, _) => await _distributedCache.SetStringAsync(headerKey, headerValue));
}
else
{
await _distributedCache.RemoveAsync(headerKey);
await Parallel.ForEachAsync(headerKeys, async (headerKey, _) => await _distributedCache.RemoveAsync(headerKey));
}
var current = _settingsManager.Load<CspSettings>();