10

I've developed an application using .NET Core and Electron.NET and created a login form.

For login, I have created separate web API project and call the login API on login button click.

When I call API from an application, it gives an error about "Cross-Origin", so I need to register an IP Address and Port in that API but right now I am facing an issue like .net core application is run on the different-different port each time.

While call sign-in in API from the window environment it gives me below port:8001

While calling the same API from the Ubuntu it gives me port 35941. So now I am facing an issue like, we have the different project for web API and it allows us to call web API on the specific port but due to each time different port generated by the electron.net, we can not call the web API in CORS (cross-origin) and it throws an error.

How can I opt-out with this situation?

halfer
  • 18,701
  • 13
  • 79
  • 158
Chetan Sanghani
  • 1,998
  • 2
  • 20
  • 33

3 Answers3

1

There is a static class BridgeSettings in Electron.NET that gives you the port settings so you can use it to set the port in the CORS settings. For example:

app.UseCors((builder) => builder.WithOrigins($"http://localhost:{BridgeSettings.WebPort}"));
AlesD
  • 9,260
  • 16
  • 30
  • Thanks,AlesD. Let me check. – Chetan Sanghani Aug 03 '18 at 07:04
  • @alesd , I think **API** is located at **Web Serve**r and "**BridgeSettings.WebPort**" is located at **Local Server(IIS)** ,then how "**BridgeSettings.WebPort**" accessible in **AP**I ? – Hiren Patel Aug 03 '18 at 08:13
  • This is available if the API is in the Web application that uses Electron.NET. If it is an outside API on a different server then you are right you don't have the settings available there. Then one solution is to Allow all origins `(builder) => builder.AllowAnyOrigin();` but this is not really secure so it is better to provide a custom function to check that ignores the port number for example like this `app.UseCors((builder) => builder.SetIsOriginAllowed((originUrl) => originUrl.StartsWith("http://localhost")));` – AlesD Aug 04 '18 at 22:28
  • Thanks AlesD. Let me check this and I'll update you soon – Chetan Sanghani Aug 05 '18 at 05:41
  • @alesd , This will compromise security.anyone can call the API if they know actual parameters of API. – Hiren Patel Aug 06 '18 at 04:13
1

Quickest solution is to set the environmental variable with a static port (ASPNETCORE_URLS=http://+:3333) defined before dotnet start up. A great node package for setting ENV variables cross platform is cross-env - https://www.npmjs.com/package/cross-env

This may be the first of many launch settings you wish to change. You should strongly consider setting up a launchsettings.json file with these configurations. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1#development

simsom
  • 1,966
  • 1
  • 11
  • 13
1

Since version 5.22.14 of Electron.NET - Is it possible to define a separate port in the electron.manifest.json file.

An example of the manifest file: https://github.com/ElectronNET/Electron.NET/blob/master/ElectronNET.WebApp/electron.manifest.json

Add the following entry here:
"aspCoreBackendPort": 8080

Gregor Biswanger
  • 430
  • 3
  • 12