设为首页 收藏本站
查看: 1171|回复: 0

[经验分享] C#编程使用Managed Wifi API连接无线SSID

[复制链接]

尚未签到

发表于 2015-9-30 12:37:24 | 显示全部楼层 |阅读模式
  http://i.isclab.org/?p=299
  在windows平台下,可以使用native wifi api来控制无线网卡,包括获取无线网卡参数,获取周围无线接入点参数等功能,在windows xp sp2版本的系统上,使用需要下载一个KB918997补丁包才能支持,下载地址如下:http://support.microsoft.com/kb/918997/en-us。而在xp sp3、vista、win7等高版本操作系统中,已经包含了此库,所以可以直接使用。
  使用的api最好的文档当然是MSDN,地址如下:http://msdn.microsoft.com/en-us/library/ms706275(v=VS.85).aspx。里面详细介绍所用到的枚举类型、结构体、函数等,并且提供了非常好的实例代码。
  
  如何用C#去操作无线网卡连接无线网络一直是个人学习过程中的一大困惑。好在最近成功解决了这个问题。最近在写一个中国电信ChinaNet无线热点自动连接工具,期间用到了Managed Wifi API,使用起来很是方便。
  操作步骤很简单:
  1.下载Managed Wifi API
  关于Managed Wifi API:This project is a .NET class library allowing you to control Wifi (802.11) network adapters installed in your Windows machine programmatically.
  The library uses the Native Wifi API, available since Windows Vista and Windows XP SP2 (in a limited fashion, and only after applying a hotfix provided in KB article 918997). Older versions of Windows are not supported.
  2.创建C#工程文件,并添加对ManagedWifi.dll的引用。

  3.编写代码,引用“Native Wifi API”。  关键代码如下:
  



using System;
using System.Collections.Generic;
using System.Text;
using NativeWifi;

namespace ManagedWifiExample
{
class MyWifi
{
public List<WIFISSID> ssids = new List<WIFISSID>();
public MyWifi()
{
ssids.Clear();
}

static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.UTF8.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
}
/// <summary>
/// 枚举所有无线设备接收到的SSID
/// </summary>
public void ScanSSID()
{
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
// Lists all networks with WEP security
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
WIFISSID targetSSID = new WIFISSID();
targetSSID.wlanInterface = wlanIface;
targetSSID.wlanSignalQuality = (int)network.wlanSignalQuality;
targetSSID.SSID = GetStringForSSID(network.dot11Ssid);
//targetSSID.SSID = Encoding.Default.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm.ToString();
targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString();
ssids.Add(targetSSID);
//if ( network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.WEP )
//{
//    Console.WriteLine( "Found WEP network with SSID {0}.", GetStringForSSID(network.dot11Ssid));
//}
//Console.WriteLine("Found network with SSID {0}.", GetStringForSSID(network.dot11Ssid));
//Console.WriteLine("dot11BssType:{0}.", network.dot11BssType.ToString());
//Console.WriteLine("dot11DefaultAuthAlgorithm:{0}.", network.dot11DefaultAuthAlgorithm.ToString());
//Console.WriteLine("dot11DefaultCipherAlgorithm:{0}.", network.dot11DefaultCipherAlgorithm.ToString());
//Console.WriteLine("dot11Ssid:{0}.", network.dot11Ssid.ToString());
//Console.WriteLine("flags:{0}.", network.flags.ToString());
//Console.WriteLine("morePhyTypes:{0}.", network.morePhyTypes.ToString());
//Console.WriteLine("networkConnectable:{0}.", network.networkConnectable.ToString());
//Console.WriteLine("numberOfBssids:{0}.", network.numberOfBssids.ToString());
//Console.WriteLine("profileName:{0}.", network.profileName.ToString());
//Console.WriteLine("wlanNotConnectableReason:{0}.", network.wlanNotConnectableReason.ToString());
//Console.WriteLine("wlanSignalQuality:{0}.", network.wlanSignalQuality.ToString());
//Console.WriteLine("-----------------------------------");
// Console.WriteLine(network.ToString());
                }
}
} // EnumSSID

/// <summary>
/// 连接到未加密的SSID
/// </summary>
/// <param name="ssid"></param>
public void ConnectToSSID(WIFISSID ssid)
{
// Connects to a known network with WEP security
string profileName = ssid.SSID; // this is also the SSID
string mac = StringToHex(profileName); //
//string key = "";
//string profileXml = 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>New{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>none</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);
//string profileXml2 = "<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>Hacker SSID</name><SSIDConfig><SSID><hex>54502D4C494E4B5F506F636B657441505F433844323632</hex><name>TP-LINK_PocketAP_C8D262</name></SSID>        </SSIDConfig>        <connectionType>ESS</connectionType><connectionMode>manual</connectionMode><MSM> <security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>";
//wlanIface.SetProfile( Wlan.WlanProfileFlags.AllUser, profileXml2, true );
//wlanIface.Connect( Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName );
string myProfileXML = 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><connectionMode>manual</connectionMode><MSM><security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", profileName, mac);
ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, myProfileXML, true);
ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
//Console.ReadKey();
        }
/// <summary>
/// 字符串转Hex
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StringToHex(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.Default.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(Convert.ToString(byStr, 16));
}
return (sb.ToString().ToUpper());
}
}
class WIFISSID
{
public string SSID = "NONE";
public string dot11DefaultAuthAlgorithm = "";
public string dot11DefaultCipherAlgorithm = "";
public bool networkConnectable = true;
public string wlanNotConnectableReason = "";
public int wlanSignalQuality = 0;
public WlanClient.WlanInterface wlanInterface = null;
}
}
  4.示例程序允许枚举当前网卡接收到的所有无线SSID,并支持接入开放认证(无密码)的无线热点。
  
  5.其它
  Wifi XML配置文件请参考微软文档
  http://www.microsoft.com/networking/WLAN/profile/v1
  C#编程使用Managed Wifi API连接无线SSID示例代码下载
  ===============================================
  转载后个人遇到的问题:
  设置WIFI密码后,不能设置和打开WIFI连接问题解决方案:
  需要修改profileXml中的配置信息,要和WIFI设置的一样,不然不能设置和访问WIFI的哦!【个人测试经验】
  本人的设置



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>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>", profileName, key);
  
  ==================================================
  
  
  转载:http://i.isclab.org/?p=299

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-120938-1-1.html 上篇帖子: wifi密码破解方法总结(含破解软件下载链接) 下篇帖子: GPRS、EDGE、WIFI、CDMA、蓝牙、红外等各种接口传输速率的对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表