使用Managed WiFi API,如何在没有配置文件的情况下连接到网络?

时间:2018-08-09 15:51:39

标签: c# wifi

我正在尝试使用Managed Wifi API连接到网络。 我有密码,但是我不知道如何创建配置文件并传递加密的密码。 https://managedwifi.codeplex.com/

关于如何使用此API,使用密码并连接到网络的任何想法?

string selectedAdapter, string selectedNetwork, string userName, string securityKey...

var client = new WlanClient();
var adapter = client.Interfaces.First(i => i.InterfaceName == selectedAdapter);
var listOfAvailableNetworks = adapter.GetAvailableNetworkList(0).ToList();

var ssid = Encoding.ASCII.GetString(selectedWlan.dot11Ssid.SSID, 0, (int)selectedWlan.dot11Ssid.SSIDLength);
                            var ssidBytes = Encoding.Default.GetBytes(ssid);
                            var ssidHex = BitConverter.ToString(ssidBytes);
ssidHex = ssidHex.Replace("-", "");

var profileName = Encoding.ASCII.GetString(selectedWlan.dot11Ssid.SSID, 0, (int)selectedWlan.dot11Ssid.SSIDLength);

var mac = ssidHex;
var keyCalculator = new WpaKeyCalculator(Encoding.ASCII.GetString(selectedWlan.dot11Ssid.SSID, 0, (int)selectedWlan.dot11Ssid.SSIDLength));
var key = BitConverter.ToString(keyCalculator.GetWpaMasterKey(securityKey));
                            var profile = string.Format("<?xml version=\"1.0\"?><WLANProfile   xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name>   <SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig>  <connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>",
                                profileName, mac, key);
adapter.SetProfile(Wlan.WlanProfileFlags.AllUser, profile, true);
adapter.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);

谢谢!

0 个答案:

没有答案