This is my first time writing a post, so bear with me.
I've made a program for my work and have been asked to implement a feature in it now, where our users are able to connect to our VPN (Always-On VPN), using the program.
However, I don't seem to be able to find any information on how to actually establish the connection programmatically. I need to basically be able to click on a button that says "Connect to VPN" and it will then establish the connection, much like it does in Windows when clicking connect:
Clicking Connect in Windows
I've figured out how to see if the connection is established or not via my program:
public bool CheckForVPNInterface()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface #interface in interfaces)
{
if (#interface.Description.Contains("Always-On VPN") && #interface.OperationalStatus == OperationalStatus.Up)
{
Label_Connection.BackColor = Color.Green;
return true;
}
else
Label_Connection.BackColor = Color.Red;
}
}
return false;
}
But now just need to have the "Connect to VPN" button.
Any idea on how to do so?
NOTE: I've tried using rasdial, but that didn't work
I couldn't find much info on the subject, but I found this obscure Windows API: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/get_connected/getvpnconnected
Edit: maybe this will also help
Related
I have a usb connected MSR reader and i am trying to get it by using the sample codes proveded in here. This works fine but the problem is when i add the same code to my app it doesn't work. GetDefaultAsync returns null.
private static MagneticStripeReader _reader = null;
public static async void StartRead()
{
if (await CreateDefaultMagneticStripeReaderObject())
{
....
}
}
private static async Task<bool> CreateDefaultMagneticStripeReaderObject()
{
if (_reader == null)
{
_reader = await MagneticStripeReader.GetDefaultAsync();
if (_reader == null)
return false;
}
return true;
}
My code is like above, very similer to sample but it doesnt work. Also i've added the device capability of pointOfService. So that is not the case.
I was in the exact same situation and I spent the last 5 hours, finally I know what was going on. You are missing a capability in the Package.appxmanifest
'pointOfService' is the capability you want to include. This capability does not show in the UI and therefore I could not find any difference between my broken project and Microsoft's sample project. You can not add that capability using the UI. You have to manually add it by modifying the XML file.
The sample project by Microsoft have it too
https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/MagneticStripeReader/cs/Package.appxmanifest#L53
Make sure the card reader is in HID mode and not Keyboard emulation mode. That was one of my problems.
To do this is really wonky. MagTek has a ActiveX control on their website to assist us... because ActiveX is awful, you can only use it with InternetExplorer (it won't even work with Edge.)
go here in IE: https://www.magtek.com/changemode/
Enable active X when it pops up, and you can change from hid to keyboard and back.
I am trying to write a program using C# to act as a multipurpose tool for my company. One of the things we would like in this tool is to determine if IPv6 is enabled/binded to the local area connection network adapter on our Windows 7 machines. I'm not looking for it to have an address, just to know if it enabled or disabled on that adapter. I am unsure as to how to code this. From what I've been able to find online, it seems I should be using System.Net.Configuration and Ipv6Element to check if it is enabled, but I have no idea how to code it. I would like to be able to display if it is enabled or disabled in a text box, so I'm guessing I'd use Boolean values. Could someone point me in the right direction on this? Thanks!
You can test whether the OS supports IPv6 by using this property:
bool supportsIpV6 = System.Net.Sockets.Socket.OSSupportsIPv6;
You can query for exactly what you asked for (if IPv6 is enabled or disabled for a specific network adapter) with the following code using the System.Net.NetworkInformation namespace:
using System.Net.NetworkInformation;
// ...
NetworkInterface[] allInterfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface firstInterface = allInterfaces[0];
bool interfaceSupportsIPv6 = firstInterface.Supports(NetworkInterfaceComponent.IPv6);
Documentation on MSDN: Link
I have used this code to test it. Notice that it tests if the IPV6 is enabled, and not if the Network Card is IPV6 compatible:
public static bool InterfaceHasIpv6Enabled(NetworkInterface #interface)
{
try
{
var properties = #interface.GetIPProperties().GetIPv6Properties();
return properties.Index > -999;
}
catch (System.Net.NetworkInformation.NetworkInformationException)
{
return false;
}
catch (Exception ex)
{
throw ex;
}
}
Is it possible to add a virtual network interface with Java or C#. I need to set a bunch of IPs to my computer for a data mining app and I don't have enought NICs (I don't want to buy more yet).
I need full control of virtual cards from the app (create, delete, set IP, maybe redirect traffic).
I am not sure you have a pure Java solution for this.
You have several alternatives.
1. Use a script that will do that for you, writing this solution in perl\shell will be 4-5 lines.
2. You can open a ssh connection to your machine and run the commands from Java.
I have such utility that uses trilead-ssh2.
To get it using maven you can use:
<dependency>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>build213-svnkit-1.3-patch</version>
</dependency>
For example, this way you should open a connection:
public static Connection newConnectionWithPassword(String host, String username, String passwd) {
Connection newConn = new Connection(host);
try {
newConn.connect(); // Ignoring ConnectionInfo returned value.
newConn.authenticateWithPassword(username, passwd);
return newConn;
} catch (IOException ioe) {
newConn.close();
ioe.printStackTrace();
return null;
}
}
Then to run your command you can :
this.session = conn.openSession();
this.session.execCommand(this.cmd);
You can use an OS command to create your virtual interfaces from java now.
BTW,
You can test the results using NetworkInterface.class , that can query a netweork interface on your machine.
Good luck.
I've written a piece of software that uses an USB 3G Dongle to connect to the internet if a connection doesn't already exist.
When the software loads, it detects whether the internet is available and if not, then creates a dial up connection (via RAS) and then dials it.
If this happens for the first time, the network location dialog comes up asking the user to select whether it's home, work or public.
Is there anyway, I can either programatically set the network location of a connection, or even tell windows not to show the dialog and automatically set the location to public?
Cheers
Gavin
Edit: For ScottM
public bool Connect(bool monitorSignalUpdates)
{
RasPhoneBook rpb = new RasPhoneBook();
rpb.Open(true);
if (!rpb.Entries.Contains("3G Connection"))
{
rpb.Entries.Add(RasEntry.CreateBroadbandEntry("3G Connection", RasDevice.GetDeviceByName("HUAWEI Mobile Connect - 3G Modem", RasDeviceType.Modem), true));
}
_rd = new RasDialer();
_rd.EntryName = "3G Connection";
_rd.PhoneNumber = "*99#";
try
{
_rd.Dial();
if (monitorSignalUpdates)
{
_queryPort.DataReceived += new SerialDataReceivedEventHandler(_queryPort_DataReceived);
}
return true;
}
catch (Exception ex)
{
int i = 99;
}
return false;
}
This registry entry controls whether Windows will prompt for ("Home/Work/Public"):
HKLM\System\CurrentControlSet\Control\Network\NewNetworkWindowOff
http://technet.microsoft.com/en-us/library/gg252535%28v=ws.10%29.aspx
You can always "“Turn off Notification on New Networks” from system tray :)
And if you can do that, I'm sure there's a registry hack and/or a PowerShell API for doing the same:
http://technet.microsoft.com/en-us/library/cc725831%28v=ws.10%29.aspx
How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7 using C#?
If you can use the Mango (7.1) SDK, and if your scenario involves using sockets, there's a trivial way to get the NetworkInterfaceType/SubType information for the connection you just made:
NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();
var type = netInterfaceInfo.InterfaceType;
var subType = netInterfaceInfo.InterfaceSubtype;
No need to use the NetworkInterface.NetworkInterfaceType property (which notoriously takes up to 30sec to return); no need to trigger a hostname resolution just to determine the network type; no need to listen to network change events.
Of course, this works best in conjunction with DeviceNetworkInformation.IsNetworkAvailable or NetworkInterface.GetIsNetworkAvailable() - those calls return immediately whether you're on a network or not. If you are, you connect the socket first and ask questions when it's connected :-)
A final note: beware of Mango's DeviceNetworkInformation.IsWiFiEnabled - I thought it would return whether I was on a wifi network, but instead it returns whether wifi is turned on or off in the phone settings... not super useful.
take a look at phoney tools, they have class PhoneNetworking for this:
http://wildermuth.com/2011/03/05/Phoney_Tools_Updated_(WP7_Open_Source_Library)
its open source you can check the source code
As of the Mango release (beta 2 and RC), this information is now available but it requires you to actually make a connection, presumably because it doesn't check until something needs it.
You can either perform a DNS resolution (see below) or use the GetCurrentNetworkInterface WebRequest extension method, which will throw an InvalidOperationException if the request hasn't connected yet.
There are also some events to follow in the Microsoft.Phone.Net.NetworkInformation namespace, but I wouldn't be surprised if those events didn't fire until a connection was made.
Interestingly, it seems you can also prefer or require on a per-connection basis using the SetNetworkPreference and SetNetworkRequirement extension methods, though it doesn't go beyond wifi vs cellular.
DeviceNetworkInformation.ResolveHostNameAsync(
new DnsEndPoint("microsoft.com", 80),
new NameResolutionCallback(nrr =>
{
var info = nrr.NetworkInterface;
var type = info.InterfaceType;
var subType = info.InterfaceSubtype;
}), null);
The enumeration values for NetworkInterfaceType (wifi/gsm) and NetworkInterfaceSubType (edge/3g) are available on MSDN.
Without socket:
var currentList = new NetworkInterfaceList().Where(i => i.InterfaceState == ConnectState.Connected).Select(i => i.InterfaceSubtype);
if (currentList.Contains(NetworkInterfaceSubType.WiFi))
Debug.WriteLine("WiFi");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
NetworkInterfaceSubType.Cellular_EVDO,
NetworkInterfaceSubType.Cellular_3G,
NetworkInterfaceSubType.Cellular_HSPA,
NetworkInterfaceSubType.Cellular_EVDV,
}).Any())
Debug.WriteLine("3G");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
NetworkInterfaceSubType.Cellular_GPRS,
NetworkInterfaceSubType.Cellular_1XRTT,
NetworkInterfaceSubType.Cellular_EDGE,
}).Any())
Debug.WriteLine("2G");
Unfortunately the api's don't provide very limited information about the kind of network connection you have. You can tell if you are on 3G, Cellular or Ethernet (i.e. USB connection to PC) but that is all the information you get.
Check out this for more info Better way to check for an network connection on WP7
To get Network Data for windows phone app i.e it is connected to a ethernet, wifi or cellular network also getting the subtype i.e 2G or 3g network following program can be used.
Using Microsoft.Phone.Net.NetworkInformation
Using Microsoft.Phone.net.NetworkInfromation
var Newlist = new NetworkInterfaceList();
foreach (NetworkInterfaceInfo x in Newlist)
{
if(x.InterfaceState==ConnectState.Connected)
{
if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.WiFi))
{
Interface = x.InterfaceType.ToString();
SubInterface = x.InterfaceSubtype.ToString();
break;
}
else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDO) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_3G) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_HSPA) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDV))
{
Interface = x.InterfaceType.ToString();
SubInterface= “3G Network”;
break;
}
else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_GPRS) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_1XRTT) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EDGE))
{
Interface = x.InterfaceType.ToString();
SubInterface= “2G Network”;
break;
}
else
{
Interface = “Ethernet”;
SubInterface= “Unknown” ;
break;
}
}
else
{
Interface=”not connected”;
SubInterface=”unknown”;
}
Here, Interface and SubInterface gives the network information.