Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RemoveUnnecessaryPortsStep #14744

Merged
merged 3 commits into from
Nov 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,46 @@ private static void RemoveUnnecessaryHttpApiHostPorts(ProjectBuildContext contex
return;
}

var portsToRemoveFromCors = new List<string>();
var lines = httpApiHostAppSettings.GetLines();
var newlines = new List<string>();
var portsToRemove = new List<string>();
var removeAngularUrl = false;

var appSettingsJson = JObject.Parse(httpApiHostAppSettings.Content);
var appJson = (JObject)appSettingsJson["App"];

if (context.BuildArgs.UiFramework != UiFramework.Angular)
{
var clientUrl = appJson.Property("ClientUrl")?.ToString();
portsToRemoveFromCors.Add("http://localhost:4200");

if (!clientUrl.IsNullOrWhiteSpace())
{
httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(clientUrl, string.Empty));
}
removeAngularUrl = true;
portsToRemove.Add("http://localhost:4200");
}

if (context.BuildArgs.UiFramework != UiFramework.Blazor)
{
portsToRemoveFromCors.Add("https://localhost:44307");
portsToRemove.Add("https://localhost:44307");
}


if (appJson["CorsOrigins"] != null)
foreach (var line in lines)
{
var corsOrigins = appJson["CorsOrigins"].ToString();
var newCorsOrigins = string.Join(",", corsOrigins.Split(',').Where(x => !portsToRemoveFromCors.Contains(x)));
var newLine = line;
if(removeAngularUrl && (line.Contains("AngularUrl")|| line.Contains("ClientUrl")))
{
continue;
}

httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(corsOrigins, newCorsOrigins));
if(line.Contains("CorsOrigins") || line.Contains("RedirectAllowedUrls"))
{
var jsonValue = line.Contains("CorsOrigins") ? appJson["CorsOrigins"]?.ToString() : appJson["RedirectAllowedUrls"]?.ToString();
if(!jsonValue.IsNullOrWhiteSpace())
{
newLine = line.Replace(jsonValue, string.Join(",", jsonValue.Split(',').Where(x => !portsToRemove.Contains(x))));
}
}

newlines.Add(newLine);
}

httpApiHostAppSettings.SetLines(newlines);
}

private static void RemoveUnnecessaryDbMigratorClients(ProjectBuildContext context)
Expand Down