1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
using System;
using System.Collections.Generic;
using NativeWifi;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.Xml;
using System.IO;
namespace TestAPI
{
class Program
{
static WlanClient mWifi = new WlanClient();
static NetworkInterface[] ListInterfaces;
static string ConvertToHex(string val)
{
string XX = string.Empty;
Char[] mtab = val.ToCharArray();
for (int i = 0; i < mtab.Length; i++)
{
XX += string.Format("{0:X}", (int)mtab[i]);
}
return XX;
}
static string CreateProfile(string reseau, string SSID, string PWD)
{
StringBuilder xmlProfileString = new StringBuilder(100);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create(xmlProfileString, settings);
writer.WriteStartElement("WLANProfile", @"http://www.microsoft.com/networking/WLAN/profile/v1");
writer.WriteElementString("name", "profileCRYO");
writer.WriteStartElement("SSIDConfig");
writer.WriteStartElement("SSID");
writer.WriteElementString("hex", ConvertToHex(SSID));
writer.WriteElementString("name", SSID);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteElementString("connectionType", "ESS"); // connection type = Infrastructure
writer.WriteElementString("connectionMode", "auto"); // connection type = Infrastructure
writer.WriteElementString("autoSwitch", "false"); // connection type = Infrastructure
writer.WriteStartElement("MSM");
writer.WriteStartElement("security");
writer.WriteStartElement("authEncryption");
writer.WriteElementString("authentication", "WPA2PSK");
writer.WriteElementString("encryption", "AES");
writer.WriteElementString("useOneX", "false");
writer.WriteEndElement();
writer.WriteStartElement("sharedKey");
writer.WriteElementString("keyType", "passPhrase");
writer.WriteElementString("protected", "false");
writer.WriteElementString("keyMaterial", PWD);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
StreamWriter SW = new StreamWriter("D:\\TEST2.xml");
SW.Write(xmlProfileString.ToString());
SW.Flush();
SW.Close();
return xmlProfileString.ToString();
}
static void Main(string[] args)
{
string NomReseau = "profileTEST";
string XmlProfile = CreateProfile(NomReseau, "<NOM_SSID>", "<PASSWORD_WPA2_AES>");
WlanClient.WlanInterface[] mInterface = mWifi.Interfaces;
foreach (WlanClient.WlanInterface Carte in mInterface)
{
Carte.SetProfile(Wlan.WlanProfileFlags.AllUser, XmlProfile, true);
try
{
Carte.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, NomReseau);
}
catch(Exception ee)
{
if (!ee.Message.Contains("introuvable"))
{
Console.WriteLine("Erreur de connexion :");
Console.WriteLine(ee.Message + "\r\n");
}
}
break;
}
Console.WriteLine("Presser une touche pour continuer");
Console.ReadKey();
}
}
} |
Partager