How to connect WPA2-Enterprise authentication network in window application? - c#

I have created wifi scanner in .net c# window application. Right now i'm able to connect to WPA2-Personal authentication network.Now i want to connect with WPA-Enterprise Network but i'm not getting any idea how to connect to it.
Please if you have any solution so let me know
Below is my code for WPA2-Personal which is working
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
Wlan.Dot11Ssid ssid = network.dot11Ssid;
string networkName = Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
ListViewItem item = new ListViewItem(networkName);
item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString());
item.SubItems.Add(network.wlanSignalQuality + "%");
item.SubItems.Add(network.dot11DefaultAuthAlgorithm.ToString());
listView1.Items.Add(item);
}
string profileName = "Network"; // this is also the SSID
string macA = "MAC_ADDRESS";
string key = "PASSWORD";
string profileXml = string.Format("<?xml version=\"1.0\" encoding=\"US-ASCII\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>WPAPSK</authentication><encryption>TKIP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>", profileName, key); //WPA-PSK
wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
}

Related

C# SimpleWifi can't connect to an unknown network

I can't connect to wifi networks that the PC doesn't know. If I manually connect first, then the program is able to connect programmatically as long as I don't click on "Forget".
If the network is not known then the ap.Connect(authRequest) returns null.
How can I connect to a wifi network programmatically that the pc doesn't know yet?
var accessPoints = wifi.GetAccessPoints();
List<string> accessPointNames = new List<string>();
foreach (AccessPoint ap in accessPoints)
{
accessPointNames.Add(ap.Name);
string fSSID = "test1234";
if (ap.Name == fSSID)
{
AuthRequest authRequest = new AuthRequest(ap)
{
Password = "12345678"
};
if (ap.Connect(authRequest))
Console.WriteLine("connected");
else
Console.WriteLine("disconnected");
break;
}
}

BLE Scanning Devices

I have this WinForms project which utilizes the 32feet library. The following code snippet is supposed to detect the BLE devices (Bluetooth Low Energy Devices) however, it returns the normal Bluetooth devices only.
Any idea?
private void blescan()
{
{
lsb_device.Items.Clear();
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
BluetoothClient bluetoothClient = new BluetoothClient();
String authenticated;
String classOfDevice;
String connected;
String deviceAddress;
String deviceName;
String installedServices;
String lastSeen;
String lastUsed;
String remembered;
String rssi;
foreach (BluetoothDeviceInfo device in devices)
{
lbl_status.Visible = true;
authenticated = device.Authenticated.ToString();
classOfDevice = device.ClassOfDevice.ToString();
connected = device.Connected.ToString();
deviceAddress = device.DeviceAddress.ToString();
deviceName = device.DeviceName.ToString();
installedServices = device.InstalledServices.ToString();
lastSeen = device.LastSeen.ToString();
lastUsed = device.LastUsed.ToString();
remembered = device.Remembered.ToString();
rssi = device.Rssi.ToString();
string[] row = new string[] { authenticated, classOfDevice, connected, deviceAddress, deviceName, installedServices, lastSeen, lastUsed, remembered, rssi };
lsb_device.Items.Add("Device Name:" + row[4] + " || MAC Address:" + row[3]);
}
lbl_status.Text = "Scan completed.";
}
}
Here's a screenshot for the scanning Form:

Simple Net Scanner Only works Wireless but not wired

I found this C# code for simple IP net scanner which scans the connected hosts in the network and displays its physical address and IP address.It works well but only when connected with the network via WIFI. It doesn't work when connected through the network via wires.
[DllImport("iphlpapi.dll", ExactSpelling = true)]
At first, iphlpapi.dll has been imported. So, can you explain it? the rest of the code is given below.
// Use Your work Group WinNT://&&&&(Work Group Name)
DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + this.TxtWorkGroup.Text.Trim());
DomainEntry.Children.SchemaFilter.Add("computer");
// To Get all the System names And Display with the Ip Address
foreach (DirectoryEntry machine in DomainEntry.Children)
{
string[] Ipaddr = new string[3];
Ipaddr[0] = machine.Name;
System.Net.IPHostEntry Tempaddr = null;
try
{
Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);
}
catch (Exception)
{
MessageBox.Show("Unable to connect with the system :" + machine.Name);
continue;
}
IPAddress[] TempAd = Tempaddr.AddressList;
foreach (IPAddress TempA in TempAd)
{
Ipaddr[1] = TempA.ToString();
byte[] ab = new byte[6];
int len = ab.Length;
// This Function Used to Get The Physical Address
int r = SendARP((int)TempA.Address, 0, ab, ref len);
string mac = BitConverter.ToString(ab, 0, 6);
Ipaddr[2] = mac;
}
ListViewItem TempItem = new ListViewItem(Ipaddr);
this.ListHostIP.Items.Add(TempItem);
}
}
Turn off the WIFI adapter and try it again.

Change IP of IIS7 Bindings in C#

I have an IIS7 web server that runs loads of sites each with multiple bindings. Each site uses host headers AND their own IP.
Using C# I need to change the IP on all bindings to 0.0.0.0 so they listen on all the IPs.
My current code doesn't add the new bindings but clears the old ones. Any ides what I'm doing wrong?
My Code:
using (ServerManager iisServerManager = new ServerManager())
{
foreach (Site site in iisServerManager.Sites)
{
List<Microsoft.Web.Administration.Binding> bindings = new List<Microsoft.Web.Administration.Binding>();
foreach (Microsoft.Web.Administration.Binding binding in site.Bindings)
{
String protocol = binding.Protocol;
String hostHeader = binding.Host;
String ipAddress = "0.0.0.0";
Int32 port = binding.EndPoint.Port;
String bind = ipAddress + ":" + port + ":" + hostHeader;
binding.BindingInformation = bind;
bindings.Add(binding);
}
//Clear existing site bindings
site.Bindings.Clear();
Int32 bindingCount = site.Bindings.Count();
foreach (Microsoft.Web.Administration.Binding binding in bindings)
{
site.Bindings.Add(binding);
bindingCount = site.Bindings.Count();
}
bindingCount = site.Bindings.Count();
iisServerManager.CommitChanges();
}
}
You don't need to remove and then re-add the same bindings. You can just modify them in place and then commit your changes. Also, you want to use the ip address of '*' instead of '0.0.0.0' when you want to listen on all ip addresses.
This worked for me...
using (ServerManager iisServerManager = new ServerManager())
{
foreach (Site site in iisServerManager.Sites)
{
foreach (Binding binding in site.Bindings)
{
string ipAddress = "*";
int port = binding.EndPoint.Port;
string hostHeader = binding.Host;
binding.BindingInformation = string.Format("{0}:{1}:{2}", ipAddress, port, hostHeader);
}
iisServerManager.CommitChanges();
}
}

Get hardware information of Windows Server

Currently I'm using following methods to get hardware information (network adapter, processor, hdd)
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
ManagementObject dsk = new ManagementObject(#"win32_logicaldisk.deviceid=""c:""");
My app is desktop, client-server (app and db are installed on server).
This methods get information for client. Is there a way to get hardware information for some node on lan - I want to get hardware information for server?
This is a subroutine I use in order to query remote hosts (here I assume I already configured WMI on the remote computer):
public string getWMI(string[] parameters)
{
string ip = parameters[0];
string username = parameters[1];
string password = parameters[2];
string query = parameters[3];
string result = "";
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope;
options.Username = username;
options.Password = password;
try
{
scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", options);
scope.Connect();
if (scope.IsConnected)
{
ObjectQuery q = new ObjectQuery(query);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, q);
ManagementObjectCollection objCol = searcher.Get();
foreach (ManagementObject mgtObject in objCol)
{
result = result + mgtObject.GetText(TextFormat.CimDtd20);
}
}
else
{
}
}
catch (Exception e)
{
writeLogFile("WMI Error: " + e.Message);
writeLog("WMI Error: " + e.Message);
}
return result;
}
In that subroutine I use a direct query such as "select * from Win32_ComputerSystem" but you can use ManagementClass as well.
I want to get hardware information for server?
WMI can poin to another server as long as:
The eserver exposes WMI
The fireawall does not block it.
Your user account has the rights on the other server.
Simple like that.

Categories