I am developing a web app that has a cloud server (nodeJS) and a local server (C#) to interface the serial port of the PC. The web page whose origin is the cloud server makes requests to the local server and gets this error
Access to fetch at 'http://localhost:3100/connection' from origin 'http://cloud.tissuelabs.com' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `local`
I tried adding headers to the request
'headers': {'Access-Control-Allow-Origin': '*'}
but it doesn`t solve my problem. I solved this problem in python using CORS libraries, but I still want to solve this problem in C# (because of performance). This is my server in C#
private void InitializeServer()
{
myServer = new Thread(Server);
myServer.Start(this);
}
private void Server(Object _obj)
{
Route.Add((req, props) => req.Url.LocalPath.ToLower().EndsWith("connection"),
(req, res, args) =>
{
if (req.HttpMethod == "GET")
{
Regex rx = new Regex(@"port=(?<word>\w+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches = rx.Matches(req.Url.Query);
if (matches.Count > 0)
{
bool on = true;
bool printing = true;
WriteLog($"GET {req.Url.LocalPath + req.Url.Query}");
WriteLog($"{matches[0].Groups["word"].Value}");
string output = "{" + $"'on':{on}, 'printing':{printing}, 'queue': {10}" + "}";
res.AsText(output);
}
else
{
WriteLog($"GET {req.Url.LocalPath}");
string[] ports = SerialPort.GetPortNames();
string output = "{'ports':[";
foreach (string port in ports)
{
output += "{" + $"'port':'{port}', 'name':'{port}'" + "}";
}
output += "]}";
res.AsText(output);
}
}
else if (req.HttpMethod == "POST")
{
WriteLog("POST /connection");
Stream stream = req.InputStream;
StreamReader sr = new StreamReader(stream);
JsonSerializer serializer = new JsonSerializer();
Connection con = (Connection)serializer.Deserialize(sr, typeof(Connection));
connectSerial(con.port);
res.AsText("{'ok': True}");
}
else if (req.HttpMethod == "DELETE")
{
WriteLog("DELETE /connection");
res.AsText("OK");
}
});
HttpServer.ListenAsync(3100, System.Threading.CancellationToken.None, Route.OnHttpRequestAsync)
.Wait();
}
When I add access control allow origin to header, it works on firefox, but not on Chrome
res.AddHeader("Access-Control-Allow-Origin", "*");
Is there any library I could use with SimpleHttp? I am also using CefSharp to render the webpage. Is there any way to configure chromium web browser to ignore CORS errors?