C# Windows Service that connects to network through VPN - c#

I have a Windows Service that must connect, via VPN, to the network in order to send emails. What I need to do (when I start the service) is check whether the VPN connection exists, if not create it, and then connect to the network.
I was looking at DotRAS which looks ideal for doing all the work, however, it doesn't seem to provide a method for creating a new VPN connection.
So my question is, is there a way in which I can programmatically create a VPN connection?

Use a scriptable VPN client, and run it using Process.Start("path-to-your-script").
Depending on which client you use, you have different possibilities regarding monitoring the status of the VPN connection.
Here is an article where the Windows rasphone client is used in such a way.

My mistake, turns out DotRAS does have a method for creating new VPN connections :)

Related

Unable to connect and get data from remote local Rest API with VPN proxy

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.
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.

use socket to send data on internet

When I send data using socket in C# on LAN, everything works fine, but how do I send my data over the internet. How to send while the sever I create uses the ip from my compute (private ip)
Can someone suggest on how can I achieve this, basically I should be able to send data anywhere over the internet, not just on LAN.
These days most computers have a router with a firewall between them. Routers, via secreuity design, stop direct access to the computers behind them and their local network.
Yes you will either have to
configure the router to map through a specific port to one of your computers.
Or, the more common way to do this is using a central hub, ie a Web service as an intermediary. This way no firewall are needed as both computers are only connecting one way (out). You could use a wcf service or Web api or many technologies to achieve this and usually you'd use a database to store you game state which makes it persistent

Access data on client from server

Please actually read my post before placing it on hold!!
Let me start by saying I've been searching for a solution all afternoon and so far I have seen plenty of examples for WCF but none that would do what I need.
I have developed an application in c# that will be installed on customer servers and accesses a sql server on the customer's local network. The application also has the ability to control network relays on the customer's local network and records the status of these in sql. I am trying to figure out a way to have the customer's server establish a connection to our datacenter and be able to issue commands back to the customer's server (retrieve datasets from sql, control the network relays, etc). I have found plenty of ways to have a client call classes on a server but have so far been unsuccessful in finding the reverse. One consideration was writing a web service as part of the application on the customer's server but need a way to establish this connection for customers with dynamic IP addresses and without having to publish through firewalls, etc.
Have you considered using
VPN - Virtual private network
or
Configuring a Port Forwarding redirect on the ADSL modem, and using a solution like www.noip.com ?
If I understand correctly you want to get information from the customer's database, which is behind a firewall and has no known static ip, in addition there might be several hundred customers so a dedicated VPN to the customer is not viable.
First of all: you should not contact the customer database directly. Databases are not designed for this scenario and would probably be left open to attack if exposed directly to the internet.
So you need a service on top of the database. There are two main options you can use for this service:
Polling service
The service is actually a client calling some web service on your network and asking for instructions.
Benefits: easy to implement and deploy.
Downsides: With polling there is always the cost-benefit of scalability/bandwidth use vs. speed of service. There are also some considerations in selecting the time to poll to prevent all the client polling at the same time.
The service is a tcp-server
This can be a usual web service (or RESTfull service) or some other service. The only difference is that it needs to advertise itself. For that you need to have a known directory server. When the service starts it then connects to the directory service and tells it the port it can be contacted on (the directory knows the ip from the connection). It will then need to periodically contact the directory to let it know it is still alive and so any change in IP is detected.
A client on your network would now query the directory to find the address of the client and connect directly to it to issue commands.
Benefit: Scalable and bandwidth efficient.
Downside: More difficult to implement. Requires firewall traversal solutions (UPNP or firewall exceptions).

Client/Server communication over the internet using ZeroMQ

I am new to zeroMQ. I am trying to develop a simple client server application and I am following the tutorials on zeroMQ’s website:
Server: http://zguide.zeromq.org/cs:wuserver
Client: http://zguide.zeromq.org/cs:wuclient
It’s working fine when I am trying to connect to the server using Local Host Loop Back IP(127.0.0.1) or internal LAN IP but when I attempt to connect over the Internet, i do not receive any messages on the client side.
I have a couple questions:
1- Is this even possible? If not then is there any better way to implement the publisher/subscriber messaging model?
2- Am i doing anything wrong? Do i need to do something differently for communication over the internet?
Hoping to get some positive feedback.
Regards.
You probably have a firewall that has blocked external connections to the port you're using. You might try looking at the admin for your router () and opening a certain port to use for testing.

Is there a difference between connect PPC to WS through ActiveSync and IP cradle?

i build C# program on Windows-Mobile that connect to WebService through cradle
and work with ActiveSync.
the customer want me to change the program that the connection will be through IP cradle.
is it require me any code change ?
how to do it ?
thank's in advance
It shouldn't require any code change. If you can get Internet Explorer on your device to display your web service page (asmx), then your application should be able to connect to the web service as well.
I don't think so, web requests should use connection manager which will automatically use the ip connection that is already there.
It's probably worth noting here that the ActiveSync pass-through is not a full-up network bridge. For example, ICMP packets don't pass through it. For a Web Service call using port 80 HTTP commands, however, you should be fine.

Categories