0

I'm trying to connect to a remote local server to get data from their Rest API. I'm on a ASP.NET MVC project and using RestSharp. When I establish VPN connection to the remote local server (via default Windows VPN connection), I'm be able to use Postman and successfully get data from their APIs. I will use this data to add new records to my DB or only display to users in my project.

But without VPN connection, I can't connect to the remote local server with Restsharp. It gives me timeout error. My C# codes for RestSharp configurations is like:

    public async Task<ActionResult> PersonalList()
    {
        var options = new RestClientOptions("http://5.2.xxx.xx") //Remote local server IP
        {
            Proxy = new WebProxy("192.168.1.240", 12001), //Remote local API url and port
            ThrowOnAnyError = true
        };
        var client = new RestClient(options);
        client.Authenticator = new HttpBasicAuthenticator("myUserNameWhichIuseAtWindowsVPNConnection", "myPasswordWhichIuseAtWindowsVPNConnection");

        var req = new RestRequest("/datasnap/rest/ServerMethods/GetPersonalList", Method.Get); //Get personal list API url
        var resp = await client.ExecuteAsync(req);

        return View();
    }

I haven't changed or add any configuration to function or web.config for default credientals.

Now, I'm confused about "Am I trying the right way? Or is this type of connection to remote local server is impossible?". If this is wrong way, how should I deploy this project? If I deploy it to their local server, they only use it locally (In additon, I can't use their internet connection when I connect to their local server via VPN. I can use only their "192.168.1.240:12001".). I want them to use it when they are out of their company, not only locally.

Maybe I mixed a lot of different problems because of my confussion, sorry for that. But I will be very glad if you could suggest me what is the best practice for the solution of this situation.

kkakkurt
  • 2,790
  • 1
  • 30
  • 33

2 Answers2

1

Not to be harsh here but a quick terminology correction... When you write: "remote local server" I think what you're saying is that you want to connect to a remote server 'remote server' from your 'local computer' over a VPN. You will only be able to do that when your VPN client is active. Putting both words together like that, "remote local", is to me at least, confusing. :)

When the VPN client is on - it will modify your local operating system and provide a mechanism (exact mechanism depends on your VPN) for your operating system to understand that the IP "5.2.xxx.xx" is somehow addressable from your local computer. Your operating system will pick up packets that you tell it to send to "5.2.xxx.xx" and it will transport them to the other side of your VPN. Once at the other side, the VPN knows how to send your traffic to the actual target.

When you turn your VPN off - you lose that "magic" the VPN is providing for your operating system. Suddenly your OS doesn't understand how to send IP packets from your local computer to "5.2.xxx.xx". That will result in your code returning some kind of "I can't connect" (like a timeout, or 'no route to host' or some error like that)

If this is wrong way, how should I deploy this project?

Hard to answer this question. You could expose the server to the open internet, but I suspect that's specifically what you don't want to do since you're using a VPN?

If I deploy it to their local server, they only use it locally (In additon, I can't use their internet connection when I connect to their local server via VPN. I can use only their "192.168.1.240:12001".).

If you can't use "their" internet connection when connected to the VPN (I would think you mean YOUR internet connection?) it sounds like the VPN is not a "split-tunnel". That means ALL your traffic is going through their VPN and your traffic is probably getting intercepted by the VPN's security rules (and blocking you).

I want them to use it when they are out of their company, not only locally.

They would need to be on the VPN or else they won't have access. That's how it works.

This is a 'pretty big' topic you're asking about. It probably would be worthwhile for you to watch some youtube videos about how VPNs work and IP networking in general. I think that'd help you understand. :| I know that's not a great answer but it's what you probably should do.

Clint D
  • 81
  • 3
  • Although I asked in a confusing way, thank you very much for this enlightening answer you gave. I tried to learn if I can set VPN connection credientials into my code and then; when there is a request occurred in my project to API, then it will automatically establish VPN connection and get neccessary data from API. But it seems technically impossible. So, I think they have to choose one of these options: 1- They could expose their server to the open internet. 2- I will deploy this project to their server and they would need to be on the VPN when they want to use this project. – kkakkurt Apr 25 '22 at 11:05
0

A way to go about using rasdial (you can use netsh command as well)

    public bool ConnectToVPN (string vpnName, string username, string password)
{

    var process = new Process
    {
        StartInfo =
        {
            FileName = "rasdial",
            Arguments = $"{vpnName} {username} {password}",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    process.Start();
    process.WaitForExit();

    var output = process.StandardOutput.ReadToEnd();
    return output.Contains("successfully connected");
}

Don't forget to use a try-catch block to enable you catch errors as would want.